从另一个线程调用RelayCommand InvalidOperationException

时间:2014-10-07 17:41:23

标签: c# wpf mvvm relaycommand

我在我的WPF应用程序的视图模型中使用了TeamFoundation.MVVM.RelayCommand。有些用户在使用过程中会收到随机的InvalidOperationException。很抱歉,除了调用堆栈之外,我无法提供更多信息,因为它不可重复,但它会不时地随机发生。这是调用堆栈

System.InvalidOperationException: The calling thread cannot access this object because a different thread owns it.
   at System.Windows.Threading.Dispatcher.VerifyAccess()
   at Microsoft.TeamFoundation.MVVM.RelayCommand.remove_CanExecuteChanged(EventHandler value)
   at System.Windows.Input.CanExecuteChangedEventManager.HandlerSink.Detach()
   at System.Windows.Input.CanExecuteChangedEventManager.Purge(Object source, Object data, Boolean purgeAll)
   at MS.Internal.WeakEventTable.Purge(Boolean purgeAll)
   at MS.Internal.WeakEventTable.OnShutDown()

这是一个不同的调用堆栈

System.InvalidOperationException: The calling thread cannot access this object because a different thread owns it.
   at Microsoft.TeamFoundation.MVVM.RelayCommand.remove_CanExecuteChanged(EventHandler value)
   at System.Windows.Input.CanExecuteChangedEventManager.HandlerSink.Detach()
   at System.Windows.Input.CanExecuteChangedEventManager.Purge(Object source, Object data, Boolean purgeAll)
   at MS.Internal.WeakEventTable.Purge(Boolean purgeAll)
   at MS.Internal.WeakEventTable.OnShutDown()

命令定义如下

m_myCommand = new RelayCommand( param => DoTask(), param => m_selectedTask != null );

任何帮助表示赞赏 编辑:包含RelayCommand的UserControl是一个独立的UserControl,与主应用程序完全隔离。所有的应用程序都是创建一个Tab并将其放入其中。一旦ViewModel从xaml代码初始化RelayCommand,我的代码再也不会触及它。 selectedTask绑定到组合框,因此它也由WPF处理。

0 个答案:

没有答案