我做了一个实验来帮助我在 FlowDocumentScrollViewer、RichTextBox 和 TextBlock 中选择我更喜欢的控件。我发现 FlowDocumentScrollViewer 是最好的。
在每个窗口中,我有两个相同类型的控件:FlowDocumentScrollViewer、RichTextBox 或 TextBlock。我做了三个这样的窗口,因为 MainWindow 有三个按钮。
private void prepareButton_Click(object sender, RoutedEventArgs e)
{
document1 = HelperClass.GetDocument();
document2 = HelperClass.GetDocument();
}
private void loadButton_Click_1(object sender, RoutedEventArgs e)
{
Stopwatch watch = new Stopwatch();
watch.Start();
viewer1.Document = document1;
viewer2.Document = document2;
this.Dispatcher.BeginInvoke(DispatcherPriority.Loaded,
new Action(() =>
{
watch.Stop();
MessageBox.Show("Took " + watch.ElapsedMilliseconds + " ms",Title);
}));
}
其中 viewer1 和 viewer2 可以是 FlowDocumentScrollViewer 或 RichTextBox。对于 TextBlock,我使用
private void prepareButton_Click(object sender, RoutedEventArgs e)
{
inlines1 = HelperClass.GetInlines();
inlines2 = HelperClass.GetInlines();
}
private void loadButton_Click_1(object sender, RoutedEventArgs e)
{
Stopwatch watch = new Stopwatch();
watch.Start();
viewer1.Inlines.AddRange(inlines1);
viewer2.Inlines.AddRange(inlines2);
this.Dispatcher.BeginInvoke(DispatcherPriority.Loaded,
new Action(() =>
{
watch.Stop();
MessageBox.Show("Took " + watch.ElapsedMilliseconds + " ms");
}));
}
测试表明 FlowDocumentScrollViewer 在三者中性能最好:
FlowDocumentScrollViewer RichTextBox TextBlock
Working set 65400 67252 82124
Loading Time 1045 1414 45119