【发布时间】:2016-06-21 05:52:26
【问题描述】:
UserControls.userGridGunler ug = new UserControls.userGridGunler();//My user control
ug.Basliklar.ItemsSource = basliklar;
ug.Saatler.ItemsSource = saha.Satirlar;
TabItem ti = new TabItem();
ti.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal, (Action)delegate
{
ti.Header = saha.SahaAdı + " (" + saha.SahaTipi + ")";
ti.Content = ug;
});
//tabSahalar is my TabControl in mainWindow
tabSahalar.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal, (Action)delegate
{
tabSahalar.Items.Add(ti);//PROBLEM IS HERE
//tabSahalar.Items.Add(new TabItem { Header = "asdasdad" });//Problem no here
});
这是我的代码。我想多线程在 tabControl 中添加 tabitem。但我得到“调用线程无法访问此对象,因为另一个线程拥有它。”在“问题在这里”的地方出现错误。
【问题讨论】:
-
您的问题是非常常见的 UI 在一个线程上,而您无法从另一个线程使用它 - 请 google,并查看堆栈溢出,这有很多
-
查看侧边栏,标题为“相关”,那里已经列出了几个有趣的答案 - 我们可能会以 stackoverflow.com/questions/1792129/… 的欺骗来结束这个问题?
-
在 Invoke 操作中创建 TabItem (
ti = new TabItem();)。 -
@CaKaL 你能通过电子邮件把你的解决方案发给我吗?
-
@EngineerSpock 为什么不呢?您的电子邮件地址?
标签: c# wpf multithreading async-await ui-thread