Csharp/C#教程:加载所有项目并在ListView中显示后会触发哪个事件?分享


加载所有项目并在ListView中显示后会触发哪个事件?

在WPF ListView中加载并显示所有项目后会触发哪个事件? 我尝试优化在ListView中显示大量项目。 ListView使用以下代码填充Items:

List 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毫秒”或类似的东西。 但是在我看到屏幕上ListView中的Items之前,它需要大约3秒钟。 是否有一个事件在ListView加载项目后触发? 我有兴趣测量ListView为用户准备好的时间。

感谢您的意见。 我找到了解决方案。 在Nick Baker的评论之后,我用Google搜索了Dispatcher.Invoke。 阅读和测试后,我找到了这个页面

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

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

 private void Work(){ List 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

渲染完成后显示最后一行,并显示正确的时间。 这正是我想要的。

上述就是C#学习教程:加载所有项目并在ListView中显示后会触发哪个事件?分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/cdevelopment/997131.html

(0)
上一篇 2021年12月26日
下一篇 2021年12月26日

精彩推荐