我知道,关于这个主题已经有很多线程,但到目前为止我无法找到解决这个问题的方法。
首先,这是我的ItemsControl
:
<ScrollViewer Style="{StaticResource ScrollViewer}" VerticalScrollBarVisibility="Visible" MaxHeight="475">
<ItemsControl ItemsSource="{Binding CurrentSelectedItems}" Margin="0 10 0 0">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<VirtualizingStackPanel />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<controls:ItemControl />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
为简单起见,
ItemsSource
ItemsControl
的属性(property)绑定(bind)到 ObservableCollection
类型 ItemViewModel
.ItemViewModel
包含大约 25 个属性和一些 Prism DelegateCommands
.ItemsControl
上面还有一些按钮更改CurrentSelectedItems
的内容.由于这些项目有时总共多达 300 个,因此更新 UI 需要一些时间。在这种情况下,应用程序是 - 原因 - 卡住。有什么方法可以不卡住 UI 和 e.G.显示微调器/加载消息?
提前致谢
编辑:
controls:ItemControl
是一个简单的自定义控件,包含大约 5 个 Labels
和 3 个相当小的 Images
最佳答案
尽管有其他答案,但无法避免这种情况。
WPF 是一种 SPA(单线程单元),这意味着任何 UI 对象都必须由 UI 线程创建和拥有。当你有很多 UI 元素要构建和渲染时,UI 线程会很忙。
显示 UI 加载动画的解决方案也是不正确的,因为此动画也将在 UI 线程上,该线程已经忙于创建和呈现您的 UI 集合。
只需确保任何处理都在后台线程上,并让 UI 自行管理。
关于c# - 更新 ObservableCollection 时 UI 卡住,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48938990/