在加载所有项目并将其显示在ListView中后,将触发哪个事件? [英] Which event fires after all items are loaded and shown in a ListView?

查看:88
本文介绍了在加载所有项目并将其显示在ListView中后,将触发哪个事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在加载所有项目并将其显示在WPF ListView中之后,将触发哪个事件? 我尝试优化在ListView中显示很多项目.使用以下代码的Items填充ListView:

Which event fires after all items are loaded and shown in a WPF ListView? I try to optimize showing lots of Items in a ListView. The ListView is populated with Items with the following code:

List<Artist> selectedArtistsList;
//Code to fill selectedArtistsList with about 6,000 items not shown here
CollectionViewSource selection1ViewSource = ((CollectionViewSource)(this.FindResource("selection1Source")));
Stopwatch stopWatch1 = new Stopwatch();
stopWatch1.Start();
selection1ViewSource.Source = selectedArtistsList;
stopWatch1.Stop();
Debug.Print("Time used: {0}ms", stopWatch1.ElapsedMilliseconds.ToString());

运行此代码时,我看到使用时间119毫秒"或类似的内容.但是,大约要花3秒钟以上的时间,我才能在屏幕上看到ListView中的项目. 在将ListView加载到Items后,是否会触发一个事件? 我有兴趣衡量ListView何时为用户准备就绪.

When I run this code I see "Time used 119ms" or something similar. But then it takes about 3 seconds more before I see the Items in the ListView on the screen. Is there an event which fires after the ListView is loaded with the Items? I am interested to measure when the ListView looks ready for the user.

推荐答案

感谢您的评论.我找到了解决方案. 在尼克·贝克发表评论后,我用Google搜索了Dispatcher.Invoke.经过阅读和测试后,我找到了此页面

Thanks for your comments. I found a solution. After Nick Baker’s comment I googled Dispatcher.Invoke. After reading and testing I found this page

WPF:Window渲染完成时运行代码 http://geekswithblogs .net/ilich/archive/2012/10/16/running-code-when-windows-rendering-is-completed.aspx

WPF: Running code when Window rendering is completed http://geekswithblogs.net/ilich/archive/2012/10/16/running-code-when-windows-rendering-is-completed.aspx

然后,我将代码更改为以下代码(不是完整的代码,只是相关部分):

Then I changed my code to the following (not the complete code, just the relevant parts):

private void Work(){
    List<Artist> selectedArtistsList;
    //Code to fill selectedArtistsList with about 6,000 items not shown here
    CollectionViewSource selection1ViewSource = ((CollectionViewSource)(this.FindResource("selection1Source")));
    stopWatch1.Reset();
    stopWatch1.Start();
    selection1ViewSource.Source = selectedArtistsList;
    Debug.Print("After setting Source: {0}ms", stopWatch1.ElapsedMilliseconds.ToString());

    //New:
    Dispatcher.BeginInvoke(new Action(RenderingDone), System.Windows.Threading.DispatcherPriority.ContextIdle, null);
}

//New
Stopwatch stopWatch1 = new Stopwatch();
private void RenderingDone() {
    Debug.Print("After rendering is done: {0}ms", stopWatch1.ElapsedMilliseconds.ToString());
}

运行此命令后,我看到: 设定后来源:124ms 渲染完成后:2273ms

After running this I see: After setting Source: 124ms After rendering is done: 2273ms

渲染完成后出现最后一行,它显示正确的时间.这正是我想要的.

The last line appear after the rendering is done and it shows the correct time. This is exactly what I wanted.

这篇关于在加载所有项目并将其显示在ListView中后,将触发哪个事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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