如何计时ListBox加载时间WPF [英] How to time ListBox load time WPF
问题描述
我正在玩DataVirtualization和Async.我可以使用哪些选项来量化将虚拟化数据集合绑定到的ListBox的加载时间?
我需要一种比较虚拟化和非虚拟化数据加载的方法.找不到有关该主题的任何资源.
我应该在后面的代码中将秒表放在ListBox_Loaded事件上吗?
提前谢谢!
您可以为此使用System.Diagnostics.Stopwatch
.确保在ListBox.Loaded
事件中之前,在 之前启动它,并设置ListBox.ItemsSource
属性并停止它:
在XAML中:
<ListBox Name="ListBox" />
在代码构造函数中:
public MainWindow()
{
InitializeComponent();
ListBox.Loaded += new RoutedEventHandler(ListBox_Loaded);
Items.AddRange(Enumerable.Range(1, 100000000));
stopwatch = new Stopwatch();
stopwatch.Start();
ListBox.ItemsSource = Items;
}
在调用后添加断点 的处理程序以停止Stopwatch
:
private void ListBox_Loaded(object sender, RoutedEventArgs e)
{
stopwatch.Stop();
TimeSpan elapsedTime = stopwatch.Elapsed;
}
但是,除非您有数百万行的数据或极其复杂的DataTemplates
,否则您可能不会看到太多差异.在这个简单的示例中,这100,000,000个数字在不到一秒钟的时间内就得到了处理.即使当我为整数添加较大的DataTemplate
时,它仍会在不到一秒钟的时间内呈现所有整数.此外,重复运行此方案将返回不同的结果,因此这也不可靠.
I'm playing around with DataVirtualization and Async. What are some options I have for quantifying load times of a ListBox that I'm binding my virtualized data collections to?
I need a way to compare the virtualized vs non-virtualized data loading. Have been unsuccessful in locating any resources for this topic.
Should I just put a stopwatch on the ListBox_Loaded event in the code behind?
Thanks in advance!
You can use a System.Diagnostics.Stopwatch
for this. Make sure that you start it before you set the ListBox.ItemsSource
property and stop it as you said, in the ListBox.Loaded
event:
In XAML:
<ListBox Name="ListBox" />
In code constructor:
public MainWindow()
{
InitializeComponent();
ListBox.Loaded += new RoutedEventHandler(ListBox_Loaded);
Items.AddRange(Enumerable.Range(1, 100000000));
stopwatch = new Stopwatch();
stopwatch.Start();
ListBox.ItemsSource = Items;
}
Add the handler with a break point after the call to stop the Stopwatch
:
private void ListBox_Loaded(object sender, RoutedEventArgs e)
{
stopwatch.Stop();
TimeSpan elapsedTime = stopwatch.Elapsed;
}
However, unless you have millions of rows of data, or extremely complicated DataTemplates
, you may not see much differences. In this simple example, these 100,000,000 numbers are processed in well under one second. Even when I added a larger DataTemplate
for the integers, it still rendered them all in just over one second. Furthermore, repeatedly running this scenario will return differing results, so this is somewhat unreliable as well.
这篇关于如何计时ListBox加载时间WPF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!