富文本框突出显示性能



我正在开发根据正则表达式模式突出显示富文本框中的文本的应用程序。它工作正常,除了性能,即使对于小文本(约 500 个字符),它也会挂起一段时间,这对用户可见。

我在使用流文档时做错了什么吗?有人可以指出我性能问题的根源吗?

    public class RichTextBoxManager
{
    private readonly FlowDocument inputDocument;
    private TextPointer currentPosition;
    public RichTextBoxManager(FlowDocument inputDocument)
    {
        if (inputDocument == null)
        {
            throw new ArgumentNullException("inputDocument");
        }
        this.inputDocument = inputDocument;
        this.currentPosition = inputDocument.ContentStart;
    }
    public TextPointer CurrentPosition
    {
        get { return currentPosition; }
        set
        {
            if (value == null)
            {
                throw new ArgumentNullException("value");
            }
            if (value.CompareTo(inputDocument.ContentStart) < 0 ||
                value.CompareTo(inputDocument.ContentEnd) > 0)
            {
                throw new ArgumentOutOfRangeException("value");
            }
            currentPosition = value;
        }
    }
    public TextRange Highlight(string regex)
    {
        TextRange allDoc = new TextRange(inputDocument.ContentStart, inputDocument.ContentEnd);
        allDoc.ClearAllProperties();
        currentPosition = inputDocument.ContentStart;
        TextRange textRange = GetTextRangeFromPosition(ref currentPosition, regex);
        return textRange;
    }
    public TextRange GetTextRangeFromPosition(ref TextPointer position,
                                              string regex)
    {
        TextRange textRange = null;
        while (position != null)
        {
            if (position.CompareTo(inputDocument.ContentEnd) == 0)
            {
                break;
            }
            if (position.GetPointerContext(LogicalDirection.Forward) == TextPointerContext.Text)
            {
                String textRun = position.GetTextInRun(LogicalDirection.Forward);
                var match = Regex.Match(textRun, regex);
                if (match.Success)
                {
                    position = position.GetPositionAtOffset(match.Index);
                    TextPointer nextPointer = position.GetPositionAtOffset(regex.Length);
                    textRange = new TextRange(position, nextPointer);
                    textRange.ApplyPropertyValue(TextElement.BackgroundProperty, Brushes.Yellow);
                    position = nextPointer;
                }
                else
                {
                    position = position.GetPositionAtOffset(textRun.Length);
                }
            }
            else
            {
                position = position.GetNextContextPosition(LogicalDirection.Forward);
            }
        }
        return textRange;
    }
}

调用它,我首先在初始化方法中创建一个实例

frm = new RichTextBoxManager(richTextBox1.Document);

在文本框的文本更改事件(我放置正则表达式的地方)上,我调用突出显示方法

frm.Highlight(textBox1.Text);

这是一种不同的方法,但我在 200,000 个字符的文件上以亚秒级响应使用它。

由于我从文本开始,因此这可能不适合您。

我索引文本文件的单词位置,用户可以搜索单词。 我突出显示他们搜索的单词。

但是我循环浏览文本以创建 FlowDoc,并在构建 FlowDoc 时突出显示。 此 FlowDoc 没有格式(除了突出显示)。 因此,如果您需要保留格式,这将不起作用。

所以我的猜测是TextPointer 开销很大。

但是我从你的代码中学到了很多东西,因为这就是我将尝试和突出显示工作的方式,但我根本无法使 TextPointer 工作。

也许看看在textRun中处理所有匹配项,而不仅仅是第一个并增加位置。

最新更新