WPF

时间:2017-03-28 07:26:52

标签: c# wpf observablecollection dispatch

我有一个应用程序的扩展,所以我的程序实际上是一个在WPF中创建的DLL。此扩展适用于Windows资源管理器。  所以,当我第一次打开时,一切似乎都很好,但我打开一个新的Windows资源管理器,然后一个不同的线程正在正常进行处理。但它会导致问题。我使用Dispatcher调用,但我仍然遇到同样的问题

//如有必要,延迟加载子项。

    if (!this.HasLoadedChildren)
    {

       try
       {
        Trace.WriteLine("Thread ID: "+ Dispatcher.CurrentDispatcher.Thread.ManagedThreadId);
            this.Children.Remove(DummyChild);
        }
        catch (Exception e)
        {
          System.Windows.Threading.Dispatcher.CurrentDispatcher
          .BeginInvoke(System.Windows.Threading.DispatcherPriority.Normal,
       (Action) delegate ()
       {
           this.Children.Remove(DummyChild);
       });
      }

我在开始时跟踪当前的线程ID" 1"当我为我的应用程序打开另一个资源管理器窗口时,我看到线程ID是" 3"然后我得到了这个例外。

此类型的CollectionView不支持从与Dispatcher线程不同的线程更改其SourceCollection。

  

在System.Windows.Data.CollectionView.OnCollectionChanged(Object   sender,NotifyCollectionChangedEventArgs args)at   System.Collections.ObjectModel.ObservableCollection 1.OnCollectionChanged(NotifyCollectionChangedEventArgs e) at System.Collections.ObjectModel.ObservableCollection 1.RemoveItem(的Int32   索引)在System.Collections.ObjectModel.Collection`1.Remove(T   项目)   RMView.ViewModel.TreeViewItemViewModel.set_IsExpanded(布尔值)   在C:\ Users \ xyz \ Development ... \ TreeViewItemViewModel.cs:第92行

我尝试使用Application.Current.Dispatcher但它的null,我认为因为我的应用程序是一个DLL,而不是一个Windows exe应用程序。

我在哪里做错了?

0 个答案:

没有答案