gpt4 book ai didi

c# - 在加载所有项目并将其显示在ListView中后,将触发哪个事件?

转载 作者:行者123 更新时间:2023-12-03 10:44:47 25 4
gpt4 key购买 nike

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

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为用户准备就绪的时间。

最佳答案

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

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

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

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

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

关于c# - 在加载所有项目并将其显示在ListView中后,将触发哪个事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31604457/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com