9

这是我的场景:

我有一个绑定到 BindingList 的 GridControl。起初我正在做的是创建一个工作线程并直接访问 BindingList ,但这会引发“检测到跨线程操作”,所以我按照这里的指南进行操作:

http://www.devexpress.com/Support/Center/p/AK2981.aspx

通过将原始 BindingList 克隆到工作线程并更改它,我得到了预期的效果。但是,我最近将 INotifyPropertyChanged 实现到保存在 BindingList 中的对象中,并且我再次开始收到错误。

我的猜测是 GridView 仍在侦听对象中的 INotifyPropertyChanged。

我怎样才能解决这个问题?

我的课:

public class Proxy : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    protected void OnPropertyChanged(string name)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(name));
        }
    }
4

4 回答 4

13

如果您从 UI 线程外部(例如从工作线程)操作 UI,则需要重新加入 UI 线程。您可以通过调用InvokeUI 控件来执行此操作。您可以使用InvokeRequired.

通常使用的模式是这样的:

public void ChangeText(string text)
{
   if(this.InvokeRequired)
   {
      this.Invoke(new Action(() => ChangeText(text)));
   }
   else
   {
      label.Text = text;  
   }
}

在您的情况下,UI 正在被操纵INotifyPropertyChanged,因此您需要确保始终在 UI 线程上修改实体(使用上述技术),或者使用通用异步 INotifyPropertyChanged 助手。这是被绑定项目的包装。它使用上述技术来确保ChangeProperty事件在 UI 线程上触发。

这是一个非常粗略的类代理示例Entity。这可确保属性更改事件重新加入 UI 线程,并保持实体本身不被修改。显然,您可能希望更通用地使用 DynamicObject 来实现这一点。

public class NotificationHelper : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    private readonly ISynchronizeInvoke invokeDelegate;
    private readonly Entity entity;

    public NotificationHelper(ISynchronizeInvoke invokeDelegate, Entity entity)
    {
       this.invokeDelegate = invokeDelegate;
       this.entity = entity;

       entity.PropertyChanged += OnPropertyChanged;
    }

    public string Name
    {
       get { return entity.Name; }
    }

    private void OnPropertyChanged(object sender, PropertyChangedEventArgs e)
    {
        if (PropertyChanged != null)
        {
           if (invokeDelegate.InvokeRequired)
           {
               invokeDelegate.Invoke(new PropertyChangedEventHandler(OnPropertyChanged),
                                     new[] { sender, e });
               return;
           }
           PropertyChanged(this, e);
        }
     }
 }
于 2012-04-14T20:27:02.350 回答
6

我对 TheGateKeeper 的最终解决方案采取了类似的方法。但是我绑定到许多不同的对象。所以我需要一些更通用的东西。解决方案是创建一个也实现了 ICustomTypeDescriptor 的包装器。通过这种方式,我不需要为可以在 UI 中显示的所有内容创建包装器属性。

public class SynchronizedNotifyPropertyChanged<T> : INotifyPropertyChanged, ICustomTypeDescriptor
    where T : INotifyPropertyChanged
{
    private readonly T _source;
    private readonly ISynchronizeInvoke _syncObject;

    public SynchronizedNotifyPropertyChanged(T source, ISynchronizeInvoke syncObject)
    {
        _source = source;
        _syncObject = syncObject;

        _source.PropertyChanged += (sender, args) => OnPropertyChanged(args.PropertyName);
    }

    public event PropertyChangedEventHandler PropertyChanged;
    protected virtual void OnPropertyChanged(string propertyName)
    {
        if (PropertyChanged == null) return;

        var handler = PropertyChanged;
        _syncObject.BeginInvoke(handler, new object[] { this, new PropertyChangedEventArgs(propertyName) });
    }

    public T Source { get { return _source; }}

    #region ICustomTypeDescriptor
    public AttributeCollection GetAttributes()
    {
        return new AttributeCollection(null);
    }

    public string GetClassName()
    {
        return TypeDescriptor.GetClassName(typeof(T));
    }

    public string GetComponentName()
    {
        return TypeDescriptor.GetComponentName(typeof (T));
    }

    public TypeConverter GetConverter()
    {
        return TypeDescriptor.GetConverter(typeof (T));
    }

    public EventDescriptor GetDefaultEvent()
    {
        return TypeDescriptor.GetDefaultEvent(typeof (T));
    }

    public PropertyDescriptor GetDefaultProperty()
    {
        return TypeDescriptor.GetDefaultProperty(typeof(T));
    }

    public object GetEditor(Type editorBaseType)
    {
        return TypeDescriptor.GetEditor(typeof (T), editorBaseType);
    }

    public EventDescriptorCollection GetEvents()
    {
        return TypeDescriptor.GetEvents(typeof(T));
    }

    public EventDescriptorCollection GetEvents(Attribute[] attributes)
    {
        return TypeDescriptor.GetEvents(typeof (T), attributes);
    }

    public PropertyDescriptorCollection GetProperties()
    {
        return TypeDescriptor.GetProperties(typeof (T));
    }

    public PropertyDescriptorCollection GetProperties(Attribute[] attributes)
    {
        return TypeDescriptor.GetProperties(typeof(T), attributes);
    }

    public object GetPropertyOwner(PropertyDescriptor pd)
    {
        return _source;
    }
    #endregion ICustomTypeDescriptor
}

然后在 Ui 中,我使用以下内容绑定到这个包装器:

    private void CreateBindings()
    {
        if (_model == null) return;

        var threadSafeModel = new SynchronizedNotifyPropertyChanged<MyViewModel>(_model, this);

        directiveLabel.DataBindings.Add("Text", threadSafeModel, "DirectiveText", false, DataSourceUpdateMode.OnPropertyChanged);
    }

MyViewModel 有一个“DirectiveText”属性并实现 INotifyPropertyChanged,没有特别考虑线程或视图类。

于 2015-04-27T18:33:08.093 回答
2

我进行了子类化BindingList,以便我可以检查所需的Invoke. 这样我的业务对象就没有对 UI 的引用。

public class InvokingBindingList<T> : BindingList<T>
{
  public InvokingBindingList(IList<T> list, Control control = null) : base(list)
  {
    this.Control = control;
  }

  public InvokingBindingList(Control control = null)
  {
    this.Control = control;
  }

  public Control Control { get; set; }

  protected override void OnListChanged(ListChangedEventArgs e)
  {
    if (Control?.InvokeRequired == true)
      Control.Invoke(new Action(() => base.OnListChanged(e)));
    else
      base.OnListChanged(e);
  }
}
于 2017-02-20T17:46:31.417 回答
1

以防万一有人遇到同样的问题......几个小时后我设法解决了它。这是我所做的:

基本上问题是实现 INotifyPropertyChanged 的​​对象存在于工作线程中,这在访问 UI 线程时会导致问题。

所以我所做的是将需要更新的对象的引用传递给 INotifyPropertyChanged 对象,然后对其使用调用。

这是它的样子:

    public event PropertyChangedEventHandler PropertyChanged;

    protected void OnPropertyChanged(string name)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            //If the Proxy object is living in a non-UI thread, use invoke
            if (c != null)
            {
                c.BeginInvoke(new Action(() => handler(this, new PropertyChangedEventArgs(name))));
            }
            //Otherwise update directly
            else
            {
                handler(this, new PropertyChangedEventArgs(name));
            }

        }
    }

    //Use this to reference the object on the UI thread when there is need to
    public Control C
    {
        set { c = value; }
    }

从线程中,我所做的只是:

                    prox.c = this;
                    //Logic here
                    prox.c = null;

希望这对某人有帮助!

于 2012-04-14T20:45:11.203 回答