wpf - RichTextBox的格式慢

标签 wpf performance richtextbox flowdocument

在rtb中格式化文本时,性能越来越差:

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition/>
    </Grid.RowDefinitions>
    <StackPanel Orientation="Horizontal">
        <Button Click="ApplyFormatClick">ApplyFormat</Button>
        <TextBlock x:Name="Time"/>
    </StackPanel>

    <RichTextBox x:Name="Rtb" Grid.Row="1">
        <RichTextBox.Document>
            <FlowDocument>
                <Paragraph>
                    <Run>
                        Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum
                    </Run>
                </Paragraph>
            </FlowDocument>
        </RichTextBox.Document>
    </RichTextBox>
</Grid>

后面的代码:
private readonly SolidColorBrush _blueBrush = Brushes.Blue;
private void ApplyFormatClick(object sender, RoutedEventArgs e)
{
    Stopwatch stopwatch = Stopwatch.StartNew();
    FlowDocument doc = Rtb.Document;
    TextRange range = new TextRange(doc.ContentStart, doc.ContentEnd);
    range.ClearAllProperties();
    int i = 0;
    while (true)
    {
        TextPointer p1 = range.Start.GetPositionAtOffset(i);
        i++;
        TextPointer p2 = range.Start.GetPositionAtOffset(i);
        if (p2 == null)
            break;
        TextRange tempRange = new TextRange(p1, p2);
        tempRange.ApplyPropertyValue(TextElement.ForegroundProperty, _blueBrush);
        tempRange.ApplyPropertyValue(TextElement.FontWeightProperty, FontWeights.Bold);
        i++;
    }
    Time.Text = "Formatting took: " + stopwatch.ElapsedMilliseconds + " ms, number of characters: " + range.Text.Length;
}

应用格式化需要花费一秒钟的时间,而对它进行概要分析时,罪魁祸首是:
tempRange.ApplyPropertyValue(TextElement.ForegroundProperty, _blueBrush);
tempRange.ApplyPropertyValue(TextElement.FontWeightProperty, FontWeights.Bold);

探查器的结果对我来说是非常模糊的。

我以前从未使用过FlowDocumentRichTextBox,所以我可能做错了。

最终结果应该类似于VS查找替换,它将基于可编辑的正则表达式突出显示文本中的匹配项。

有什么不同的方法可以加快速度?
(Sample on Github)

最佳答案

建议使用新格式(you can check the MSDN Magazine August 2007: WPF Flexible Content Display With Flow Documents;或最新的MSDN文章Flow Document Overview)手动构建FlowDocument,这将大大提高性能,例如如果您按以下方式手动操作,请使用您的示例,在我的机器上,它将在52毫秒内获得结果,而使用ApplyPropertyValue则需要1266毫秒:

private readonly SolidColorBrush _blueBrush = Brushes.Blue;

private void ApplyFormatClick(object sender, RoutedEventArgs e)
{
    Stopwatch stopwatch = Stopwatch.StartNew();
    FlowDocument doc = Rtb.Document;
    TextRange range = new TextRange(doc.ContentStart, doc.ContentEnd);
    Paragraph para = new Paragraph();
    string rangetem = range.Text;
    range.ClearAllProperties();

    for(int i=0; i<rangetem.Count();i+=2)
    {
        Span s = new Span() { Foreground = _blueBrush };
        Bold b = new Bold();
        s.Inlines.Add(rangetem[i].ToString());
        b.Inlines.Add(s);
        para.Inlines.Add(b);
        if(i+1<rangetem.Count())
        {
            para.Inlines.Add(rangetem[i + 1].ToString());
        }
    }
    doc.Blocks.Clear();
    doc.Blocks.Add(para);

    Time.Text = "Formatting took: " + stopwatch.ElapsedMilliseconds + " ms, number of characters: " + range.Text.Length;
}

关于wpf - RichTextBox的格式慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17852635/

相关文章:

algorithm - 找到第n个比较

javascript - 卡在 'contenteditable'

vb.net - 如何为 vb.net 的 richtextbox 中的单词制作自定义智能感知

c# - 在 C# 中将图像插入到 RTF 文档中

wpf - ContextMenu 绑定(bind)到 ObservableCollection<MenuItem> 不刷新数据

c# - 绑定(bind)时将文本添加到 TextBlock Text 属性

performance - Julia:稀疏矩阵的 View

c++ - 如何衡量应用程序每秒可以产生多少结果

带有 MahApps 的 C# WPF 应用程序无法在 Windows 2008 中显示

c# - 如何计算索引在文本框的查看区域中垂直居中的 VerticalOffset?