2012-03-02 97 views
0

我有一个视图(用户控件),其中有选项卡控件和选项卡项。当应用程序关闭时,我想删除所有选项卡项目,为此我创建了一个调用RemoveAllTabItems函数的终结器。但是,当我试图访问选项卡控件项时出现错误:“调用线程无法访问此对象,因为不同的线程拥有它。”我试图通过使用选项卡控制调度程序来修复错误,但通过执行此操作,不会调用remove函数。为了说明WPF - 完成和UI线程

样品的编号:

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)); 
} 

回答

0

调用RemoveAllTabItems直接运行,而无需使用调度程序。

+0

如上所述,我得到一个错误,因为它来自不同的线程,这就是为什么我想使用调度程序调用该函数。 – sysboard 2012-03-02 12:22:06