更新时间:2022-12-26 22:13:14
我认为它会在 Add()
上崩溃,因为您正在添加一个已经具有逻辑的 FrameworkContentElement
父母.您必须首先从其父项中删除 Block
,然后才将其添加到不同的集合中.试试这个:
I assume it crashes on Add()
because you are adding a FrameworkContentElement
which already has a logical parent. You must first remove a Block
from its parent and only then add it to a different collection. Try this:
private static FlowDocument SetRTF(string xamlString)
{
StringReader stringReader = new StringReader(xamlString);
System.Xml.XmlReader xmlReader = System.Xml.XmlReader.Create(stringReader);
Section sec = XamlReader.Load(xmlReader) as Section;
FlowDocument doc = new FlowDocument();
while (sec.Blocks.Count > 0)
{
var block = sec.Blocks.FirstBlock;
sec.Blocks.Remove(block);
doc.Blocks.Add(block);
}
return doc;
}
然后事情就简单多了.做一个:
Then things are much simpler. Just do a:
private static FlowDocument SetRTF(string xamlString)
{
StringReader stringReader = new StringReader(xamlString);
System.Xml.XmlReader xmlReader = System.Xml.XmlReader.Create(stringReader);
return XamlReader.Load(xmlReader) as FlowDocument;
}
richTextBox.Document = SetRTF(xamlString);