如何计时ListBox加载时间WPF [英] How to time ListBox load time WPF

查看:140
本文介绍了如何计时ListBox加载时间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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆