WPF:分配给 RichTextBox.Document 非常慢(7 分钟!)

Win*_*ith 5 .net c# wpf richtextbox .net-3.5

我正在构建一个FlowDocument从 XML格式化的文件。XML 格式良好,主要由 10,000 个节点组成,每个节点都有一个具有 6 个字符串值的节点。

将 XML 解析为 anXElementFlowDocument在内存中构建大约需要 5 秒。在我的应用程序FlowDocument中将分配给 a 的Document属性RichTextBox大约需要 7 分钟,并在这段时间内最大限度地使用 CPU。

这是相关的一段代码:

// The following six lines of code execute in about 5 seconds

var xml = XElement.Parse(response.Data);

PrettyXmlConverter px = new PrettyXmlConverter();
FlowDocument fd = px.Render(xml);

Paragraph p = new Paragraph();
p.Inlines.Add(new Run(response.TimeStamp.ToShortDateString() + " " + response.TimeStamp.ToLongTimeString()));
fd.Blocks.InsertBefore(fd.Blocks.ElementAt(0), p);

// This line of code takes about 7 minutes and maxes out the CPU for that time.
tbResponse.Document = fd;
Run Code Online (Sandbox Code Playgroud)

我想知道这里发生了什么。我已经对代码进行了概要分析,并看到了对非托管方法(例如fsFormatSubtrackBottomless和 )的成千上万次调用SubtrackFormatParaBottomless

任何人都可以解决这个问题,或者想出一个解决方法吗?

Win*_*ith 0

最终我也没有找到解决这个问题的方法。

我正在使用一种解决方法 - 我只是不“漂亮地打印”超过一定大小的消息。

如果有人有更好的解决方案,请随时发布。