0

我遇到了一个问题,我在 myDataModelViewModelwith之间陷入无限循环PropertyChanged event

我的结构:

public class SomeDataModel : DataModelBase
{
    public SomeDataModel()
    {

    }

    public object SomeProperty
    {
        get
        {
            //
        }
        set;
        {
            //
            OnPropertyChanged("SomeProperty");
        }
    }

    public bool SomeMethod()
    {
        SomeProperty = SomeNewObject
    }
}

public class SomeViewModel : ViewModelBase
{
    public SomeViewModel()
    {

    }

    public SomeDataModel SomeDataModel
    {
        get;
        set;
    }

    public object SomeProperty
    {
        get
        {
            return SomeDataModel.SomeProperty;
        }
        set
        {
            SomeDataModel.SomeProperty = value;
            OnPropertyChanged("SomeProperty");
        }
    }
}

两者ViewModelDataModel实现INotifyPropertyChanged

然后我有一个listenerViewModelBase的任何property变化DataModel

private void DataModel_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
    PropertyInfo toPropertyInfo = this.GetType().GetProperty(e.PropertyName);
    PropertyInfo fromPropertyInfo = DataModel.GetType().GetProperty(e.PropertyName);

    if (toPropertyInfo != null && fromPropertyInfo != null)
    {
        if (toPropertyInfo.CanWrite && fromPropertyInfo.CanRead)
        {
            toPropertyInfo.SetValue(this, fromPropertyInfo.GetValue(DataModel, null), null);
        }
    }
}

所以现在当. property_ DataModel_ 但是集合中的集合又回到了导致再次触发的原因,以无限循环结束。ViewModelBasepropertyViewModelViewModelDataModelOnPropertyChanged("SomeProperty");

  1. 有没有快速的解决方法来解决它?(注意:不能改变结构)
  2. 我是否能够创建自己的自定义类,继承自PropertyChangedEventArgs并仍然让我的INotifyPropertyChanged操作正常工作?

我刚在想:

public class CustomPropertyChangedEventArgs : PropertyChangedEventArgs
{
    public CustomPropertyChangedEventArgs(string propertyName, SomeEnum enumName)
        : base(propertyName)
    {
        //Set enum
    }
}

然后在OnPropertyChanged我想使用新的CustomPropertyChangedEventArgs并传递enum我想使用的。

public void OnPropertyChanged(string propertyName)
{
    CustomPropertyChangedEventArgs handler = this.PropertyChanged;

    if (handler != null)
    {
        handler(this, new CustomPropertyChangedEventArgs(propertyName, newEnum));
    }
}
4

2 回答 2

5

您可以检查该值是否相同并且不要更新它。这通常INotifyPropertyChanged是如何实现的。OnPropertyChanged只有当它真的改变时,你才会开火。

public object SomeProperty
{
    get
    {
        return SomeDataModel.SomeProperty;
    }
    set
    {
        if (SomeDataModel.SomeProperty != value)
        {
            SomeDataModel.SomeProperty = value;
            OnPropertyChanged("SomeProperty");
        }
    }
}
于 2012-08-16T15:47:09.890 回答
0
you can disable and enable the event to avoid loop
private void DataModel_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
    OnPropertyChanged -= DataModel_PropertyChanged;
    PropertyInfo toPropertyInfo = this.GetType().GetProperty(e.PropertyName);
    PropertyInfo fromPropertyInfo = DataModel.GetType().GetProperty(e.PropertyName);

    if (toPropertyInfo != null && fromPropertyInfo != null)
    {
        if (toPropertyInfo.CanWrite && fromPropertyInfo.CanRead)
        {
            toPropertyInfo.SetValue(this, fromPropertyInfo.GetValue(DataModel, null), null);
        }
    }
    OnPropertyChanged += DataModel_PropertyChanged;
}
于 2012-08-16T15:56:04.977 回答