C# 在ListView中加载并显示所有项后激发哪个事件?
在WPF ListView中加载并显示所有项后激发哪个事件? 我尝试优化在列表视图中显示大量项目。ListView中填充了具有以下代码的项:C# 在ListView中加载并显示所有项后激发哪个事件?,c#,.net,wpf,listview,events,C#,.net,Wpf,Listview,Events,在WPF ListView中加载并显示所有项后激发哪个事件? 我尝试优化在列表视图中显示大量项目。ListView中填充了具有以下代码的项: List<Artist> selectedArtistsList; //Code to fill selectedArtistsList with about 6,000 items not shown here CollectionViewSource selection1ViewSource = ((CollectionViewSource
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());
列出所选的ArtistsList;
//用此处未显示的约6000项填充所选ArtistsList的代码
CollectionViewSource selection1ViewSource=((CollectionViewSource)(this.FindResource(“selection1Source”));
秒表秒表1=新秒表();
stopWatch1.Start();
selection1ViewSource.Source=selectedArtistsList;
秒表1.Stop();
Print(“使用的时间:{0}ms”,stopWatch1.elapsedmillesons.ToString());
当我运行这段代码时,我看到“使用了119ms的时间”或类似的东西。但是,在我在屏幕上看到ListView中的项目之前,还需要大约3秒钟的时间。
是否存在在ListView加载项目后激发的事件?
我有兴趣测量ListView何时为用户准备就绪。谢谢您的评论。我找到了解决办法。 尼克·贝克发表评论后,我在谷歌上搜索了Dispatcher.Invoke。经过阅读和测试,我找到了这一页 WPF:在窗口渲染完成时运行代码 然后我将代码更改为以下内容(不是完整的代码,只是相关部分):
private void Work(){
列出所选人员列表;
//用此处未显示的约6000项填充所选ArtistsList的代码
CollectionViewSource selection1ViewSource=((CollectionViewSource)(this.FindResource(“selection1Source”));
秒表1.Reset();
stopWatch1.Start();
selection1ViewSource.Source=selectedArtistsList;
Print(“在设置源代码:{0}ms之后”,stopWatch1.elapsedmillesons.ToString());
//新的:
Dispatcher.BeginInvoke(新操作(RenderingDone),System.Windows.Threading.DispatcherPriority.ContextIdle,null);
}
//新的
秒表秒表1=新秒表();
私有void RenderingDone(){
Print(“渲染完成后:{0}ms”,stopWatch1.elapsedmillesons.ToString());
}
运行此命令后,我看到:
设置源后:124ms
渲染完成后:2273ms
渲染完成后将显示最后一行,它显示正确的时间。这正是我想要的。试试
listView.ItemContainerGenerator.ItemsChanged
。WPF在一个单独的线程上呈现,因此如果您想在呈现完成后运行代码,可以尝试Dispatcher.Invoke,优先级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());
}