我正在尝试从BackgroundWorker
调用一个方法,该方法将用户控件添加到ListView
:
private void AddItems(List<FileItem> fileItems) {
System.Threading.Thread.CurrentThread.SetApartmentState(System.Threading.ApartmentState.STA);
Dispatcher.BeginInvoke(new Action(() => files.ItemsSource = fileItems));
}
用户控件
files
在构造函数中成功地从fileItems
获取数据,但它抛出了The calling thread must be STA, because many UI components require this.
异常。我已经尝试逐个向所有父方法添加[STAThread]
属性,但仍然会抛出异常。我应该在哪里添加此属性?更新:
请注意,
Dispatcher.BeginInvoke(new Action(() => files.Items.Clear()));
执行正确。