【问题标题】:When do I need to implement INotifyPropertyChanged?我什么时候需要实施 INotifyPropertyChanged?
【发布时间】:2010-10-05 00:38:27
【问题描述】:

我已经看到到处都有实现这个接口的应用程序。在很多情况下,我们可以使用新的属性语法,比如

public int Foo { get; set; }

我非常喜欢。但是,为了实现这个接口,这个必须变成10行左右。这使得代码非常混乱,我不确定它是否也会影响性能。

有人能解释一下这个接口什么时候真的需要吗?

【问题讨论】:

    标签: .net inotifypropertychanged


    【解决方案1】:

    当您的数据对象需要通告(通知)某个属性已更改时,您可以实现该接口。这在使用数据绑定时尤其重要,在使用观察者模式时非常有用。

    Check here 当我有很多需要通知更改的属性时我遵循的方法。

    【讨论】:

      【解决方案2】:

      如果您想订阅属性已更改的通知,这是必要的。如果您不需要(也不需要第 3 方库),则不必实现此接口。

      【讨论】:

      • 用于属性的数据绑定通知
      【解决方案3】:

      在使用需要它的框架内的库或其他功能时,此接口是必需的。

      最常见的情况是使用带有数据绑定的 WPF 等 UI 框架。为了让 UI “知道”您的属性已更改,因此它可以反映 TextBox 的内容,例如,与其绑定的对象需要是 DependencyObject 并且属性需要是 DependencyProperty,或者你需要实现 INotifyPropertyChanged。

      这就是使 2-way 数据绑定正常工作的原因。

      话虽如此,在基类上实现这一点很常见,这可以使您的子类实现每个属性只需几行。 (您不能使用自动属性,但如果您使用基类,则可能只需要在 setter 中增加几行。)

      【讨论】:

        【解决方案4】:

        另一种方法是使用 Microsoft 的 ReactiveExtensions (Rx) 框架将所有管道工作封装到单个 Observable 对象中。

        有关如何执行此操作的示例,请参阅 this StackOverflow question and answers

        【讨论】:

          【解决方案5】:

          这样做

          public class ViewModelBase : INotifyPropertyChanged
          

          {

          protected void SetProperty<t>(ref T newValue, ref T currentValue, bool notify, string propertyName, params string[] additionalProperties)
          {
              bool changed = notify && ((newValue != null && !newValue.Equals(currentValue)) || (newValue == null && currentValue != null));
              currentValue = newValue;
              if (changed)
              {
                  OnPropertyChanged(propertyName);
          
                  if (additionalProperties != null)
                      foreach (string additionalProperty in additionalProperties)
                          OnPropertyChanged(additionalProperty);
              }
          }
          
          protected virtual void OnPropertyChanged(string propertyName)
          {
              if (this.PropertyChanged != null)
                  this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
          }
          
          public event PropertyChangedEventHandler PropertyChanged;
          

          }

          public class MyRealViewModel : ViewModelBase
          

          { 公共 int NumberOfItems { 得到 { 返回 _numItems; } set { SetProperty(ref value, ref _numItems, true, "NumberOfItems"); } }

          public bool SomeKindOfFlag
          {
              get { return _flag; }
              set { SetProperty(ref value, ref _flag, false, ""); }
          }
          
          public LightSabre WeaponOfChoice
          {
              get { return _weapon; }
              set { SetProperty(ref value, ref _weapon, true, "WeaponOfChoice", "SomeKindOfFlag", "NumberOfItems"); }
          }
          
          private bool _flag;
          private int _numItems;
          private LightSabre _weapon;
          

          }

          public class LightSabre
          

          {

          公共字符串 LightSabreName { get;放; }

          public override bool Equals(object obj)
          {
              if (obj != null && obj as LightSabre != null)
                  return ((LightSabre)obj).LightSabreName == this.LightSabreName;
          
              return false;
          }
          

          }

          它是从上面的答案中提取出来的,对我很有帮助。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2010-09-21
            • 2018-02-25
            • 1970-01-01
            • 2018-04-20
            • 2012-09-16
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多