1

我们正在开发一个 .Net 4.0 C# WPF Line-of-business 应用程序,需要向用户显示(只读)大文本文件。TextBlock如 SO 中所述,这不是一个选项,并尝试了建议的 AvalonEdit 控件。运行 AvalonEdit.Sample 独立应用程序可以在不到一秒的时间内加载一个 8k 行的 4MB 文件,但是将 AvalonEdit 嵌入TextEditor到我们的 WPF 应用程序中需要花费 20 秒,与之前的TextBlock.

UI 相当复杂,带有用于树形菜单和表单的拆分器。该表单有几个拆分器来创建可调整大小的区域,其中一个包含一个选项卡控件。其中一个选项卡项具有TextEditor来自 AvalonEdit。

XAML

<avalonEdit:TextEditor Name="Tbx" HorizontalScrollBarVisibility="Visible" VerticalScrollBarVisibility="Visible" />

CS

public void ShowFile(string path)
{
  Tbx.Text = string.Empty;
  ThreadPool.QueueUserWorkItem(o => {
      var lines = File.ReadAllLines(path).Join("\n");
      Dispatcher.BeginInvoke(() => Tbx.Text = lineas);
  });
}

加载文件时,我的笔记本电脑 i7 4 核 CPU 使用率为 33%。

TextEditor关于 AvalonEdit作为独立应用程序和自定义 WPF 应用程序中的不同行为的任何建议?

如何加载大文件(10MB,10k 行)TextEditor

更新:

视觉树:

视觉树

的属性TextEditor

文本编辑器属性

我删除了承载TextEditor所指出的 ScrollViewer,但实现了同样糟糕的性能。

更新 2: 我将其TextEditor移至新窗口以减少布局,但性能仍然很差。

新的、简化的可视化树:

视觉树 2

Grid列和行定义是*.

的属性TextEditor

文本编辑器属性

4

3 回答 3

4

查看应用程序的可视化树(Snoop可能会有所帮助)并查看 TextEditor 控件周围是否有任何 ScrollViewer。

滚动查看器为包含的控件提供无限空间,这将禁用任何 UI 虚拟化并破坏性能(编辑器需要呈现整个文档,而不仅仅是可见部分)。

如果您出于某种原因需要嵌套滚动查看器,请查看我对嵌套滚动区域问题的回答,了解如何避免为包含的控件提供无限空间。

于 2013-02-07T16:18:13.273 回答
3

我发现虚拟化不是水平执行的。在我的情况下,我有一个没有换行符的大型 XML 文件,所以水平滚动需要 10 秒以上才能呈现。一旦相同的文本被格式化(使用换行符),它就会呈现亚秒级。

于 2015-07-27T09:16:21.713 回答
0

TextDocument 类有一个名为 SetOwnerThread() 的函数,从文档中,您似乎可以临时设置不同的线程来加载和渲染文档。

以下来自 TextDocument.cs 的评论

    /// <summary>
    /// Transfers ownership of the document to another thread. This method can be used to load
    /// a file into a TextDocument on a background thread and then transfer ownership to the UI thread
    /// for displaying the document.
    /// </summary>
    /// <remarks>
    /// The owner can be set to null, which means that no thread can access the document. But, if the document
    /// has no owner thread, any thread may take ownership by calling SetOwnerThread.
    /// </remarks>
于 2013-03-16T07:32:26.080 回答