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

Edg*_*gar 2 .net c# wpf events listview

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

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());
Run Code Online (Sandbox Code Playgroud)

当我运行此代码时,我看到“使用时间为 119 毫秒”或类似内容。但是在我在屏幕上看到 ListView 中的项目之前大约需要 3 秒钟。是否有在 ListView 加载项目后触发的事件?我有兴趣测量 ListView 何时为用户准备就绪。

Edg*_*gar 7

感谢您的意见。我找到了解决方案。在 Nick Baker 发表评论后,我在 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());
}
Run Code Online (Sandbox Code Playgroud)

运行后我看到:设置源后:124ms 渲染完成后:2273ms

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