4

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

我对 FlowDocument 做错了吗?有人可以指出性能问题的根源吗?

    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;
    }
}

要调用它,我首先在 Initialize 方法中创建一个实例

frm = new RichTextBoxManager(richTextBox1.Document);

在文本框的文本更改事件(我放正则表达式的地方)我调用 Highlight 方法

frm.Highlight(textBox1.Text);
4

1 回答 1

0

这是一种不同的方法,但我将它与 200,000 个字符的文件的亚秒响应一起使用。

由于我从文本开始,这可能不是您的选择。

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

但是我循环遍历文本以创建 FlowDoc 并在构建 FlowDoc 时突出显示。此 FlowDoc 没有格式(除了突出显示)。因此,如果您需要保留格式,这是行不通的。

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

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

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

于 2012-06-05T21:27:26.123 回答