Joh*_*son 9 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>
Run Code Online (Sandbox Code Playgroud)
代码背后:
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;
}
Run Code Online (Sandbox Code Playgroud)
应用格式化需要一秒钟,在分析它时,罪犯是:
tempRange.ApplyPropertyValue(TextElement.ForegroundProperty, _blueBrush);
tempRange.ApplyPropertyValue(TextElement.FontWeightProperty, FontWeights.Bold);
Run Code Online (Sandbox Code Playgroud)
分析器结果对我来说非常不透明.
我从来没有使用FlowDocument和RichTextBox之前,所以我可能这样做非常错误的.
最终结果意味着类似于VS查找替换,它将基于可编辑的正则表达式突出显示文本中的匹配.
可以采取哪些不同的措施加快速度?(Github上的示例)
建议FlowDocument使用新的格式化手动构建您(您可以查看2007年8月的MSDN杂志:WPF灵活内容显示与流文档 ;或最新的MSDN文章流文档概述),这将显着提高性能,例如使用您的例如,如果手动如下,在我的机器上它将在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;
}
Run Code Online (Sandbox Code Playgroud)
如果RichTextBox不需要后退功能,可以通过将IsUndoEnabled设置为false来提高性能
<RichTextBox IsUndoEnabled="False">
Run Code Online (Sandbox Code Playgroud)