WPF: Accessing bound ObservableCollection fails althouth Dispatcher.BeginInvoke is used

[亡魂溺海] 提交于 2019-12-08 02:01:58

问题


I have the following:

public ICollectionView Children
{
 get
 {
  // Determining if the object has children may be time-consuming because of network timeouts.
  // Put that in a separate thread and only show the expander (+ sign) if and when children were found
  ThreadPool.QueueUserWorkItem(delegate 
  {
   if (_objectBase.HasChildren)
   {
    // We cannot add to a bound variable in a non-UI thread. Queue the add operation up in the UI dispatcher.
    // Only add if count is (still!) zero.
    Application.Current.Dispatcher.BeginInvoke(new Action(() =>
    {
     if (_children.Count == 0)
     {
      _children.Add(DummyChild);
      HasDummyChild = true;
     }
    }),
    System.Windows.Threading.DispatcherPriority.DataBind);
   }
  });

  return _childrenView; 
 }
}

It works great: HasChildren is run in a background thread which uses the dispatcher to insert its result into the variable used for the binding to the UI.

Note: _childrenView is set to this:

_childrenView = (ListCollectionView) CollectionViewSource.GetDefaultView(_children);

Problem:

If I call the Children property from another ThreadPool thread, I get a NotSupportedException in the line

_children.Add(DummyChild);

Exception text: "This type of CollectionView does not support changes to its SourceCollection from a thread different from the Dispatcher thread."

Why? I have verified that that code is executed from the Dispatcher thread.


回答1:


We've run into this problem before ourselves. The issue is twofold:

1- Make sure that any changes to the SourceCollection are on the main thread (you've done that).

2- Make sure that the creation of the CollectionView was also on the main thread (if it were created on a different thread, say in response to an event handler, this will not usually be the case). The CollectionView expects modifications to be on "its" thread, AND that "its" thread is the "UI" thread.



来源:https://stackoverflow.com/questions/4527152/wpf-accessing-bound-observablecollection-fails-althouth-dispatcher-begininvoke

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!