我有一个视图(用户控件),其中包含选项卡控件和选项卡项。当应用程序关闭时,我想删除所有选项卡项,为此,我创建了一个终结器来调用RemoveAllTabItems函数。然而,在尝试访问选项卡控件项时,我得到了一个错误:“调用线程无法访问此对象,因为另一个线程拥有它。”我试图通过使用选项卡控件dispatcher来修复错误,但这样做不会调用remove函数。
用于说明的示例代码:
private void RemoveAllTabItems()
{
IEnumerable<TabItem> tabs = this.myTabControl.GetTabItems();
foreach (TabItem tab in tabs)
TryClose(tab);
}
~MyClass()
{
this.myTabControl.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(RemoveAllTabItems));
// Already tried these:
// this.myTabControl.Dispatcher.Invoke(DispatcherPriority.Normal, new Action(RemoveAllTabItems));
// this.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(RemoveAllTabItems));
}发布于 2012-03-02 19:43:39
直接调用RemoveAllTabItems函数,不使用dispatcher。
https://stackoverflow.com/questions/9530919
复制相似问题