在 setter 中自订阅 PropertyChanged 或添加方法调用? [英] Self-subscribe to PropertyChanged or addition method call in setter?

查看:15
本文介绍了在 setter 中自订阅 PropertyChanged 或添加方法调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

也许这里已经有这样的问题了,但我没有找到.

Maybe here is already such question, but I didn't find it.

我有 MVVM 应用程序,在我的 ViewModel 中,我必须对某些属性的更改执行一些额外的操作(例如,如果 View 更改它们).您认为哪种方法更好?为什么?

I have MVVM application, and in my ViewModel I have to do some additional actions on changes of some properties (for example, if View changes them). Which approach is better on your mind and why?

1st - 向 setter 添加 AdditionalAction 调用

public class ViewModel: INotifyPropertyChanged
{
  private int _MyProperty;

  public int MyProperty
  {
    get { return _MyProperty; }
    set
    {
      if (_MyProperty == value) return;
      _MyProperty = value;
      RaisePropertyChanged(() => MyProperty);

      // --- ADDITIONAL CODE ---
      AdditionalAction();
    }
  }
}

2nd - 自行订阅 INotifyPropertyChanged

public class ViewModel: INotifyPropertyChanged
{
  public ViewModel()
  {
    // --- ADDITIONAL CODE ---
    PropertyChanged += OnPropertyChanged;
  }

  private int _MyProperty;

  public int MyProperty
  {
    get { return _MyProperty; }
    set
    {
      if (_MyProperty == value) return;
      _MyProperty = value;
      RaisePropertyChanged(() => MyProperty);
    }
  }

  void PropertyChanged(object sender, PropertyChangedEventArgs e)
  {
    // --- ADDITIONAL CODE ---
    if (e.PropertyName == "MyProperty")
      AdditionalAction();
  }
}

想象一下,我没有性能问题或 10'000 个对象.它只是视图和视图模型.什么是更好的?第一个代码更小"并且开销更少,但第二个(在我看来)更清晰,我可以使用代码片段来自动生成属性的代码.甚至更多 - 在第二种情况下,我可以在事件处理程序中编写如下内容:

Imagine, that I don't have performance problem or 10'000 objects. It's just View and ViewModel. What is better? First code is "smaller" and has less overhead, but the second (on my mind) is more clear and I can use code snippets for auto-generation properties' code. Even more - in the 2nd case I can write in event handler something like:

On.PropertyChanged(e, p => p.MyProperty, AdditionalAction);

其中 On 是类助手.

那么,您认为什么更好?为什么?

So, what is better on your mind and why?

更新:

好的,我似乎找到了一种方法:

OK, it looks like I found yet one approach:

3rd - 在 RaisePropertyChanged 中添加扩展点":

public class NotificationObject : INotifyPropertyChanged
{
  void RaisePropertyChanged(Expression<...> property)
  {
    // ... Raise PropertyChanged event
    if (PropertyChanged != null)
      // blah-blah

    // Call extension point
    OnPropertyChanged(property.Name);
  }

  public virtual OnPropertyChanged(string propertyName)
  {
  }
}

public class ViewModel: NotificationObject
{
  private int _MyProperty;

  public int MyProperty
  {
    get { return _MyProperty; }
    set
    {
      if (_MyProperty == value) return;
      _MyProperty = value;
      RaisePropertyChanged(() => MyProperty);
    }
  }

  override OnPropertyChanged(string propertyName)
  {
    if (propertyName == "MyProperty")
      AdditionalAction();
  }
}

这样我们不使用事件,而是从同一个扩展点"调用所有附加操作".所有添加操作的一个地方"比不透明的工作流程"更好吗?

This way we don't use event, but all "additional actions" are called from the same "extension point". Is "one place for all addition actions" better than "not transparent workflow"?

推荐答案

肯定会选择第一种方法:

  • 很清楚
  • 它的流程和意图很明确
  • 它避免了奇怪的(imo)自我订阅

第二个的好处",它允许您使用自动生成的属性,不值得第一个案例 imo 的执行流程清晰.

The "benefits" of second, which lets you use autogenerated properties is not worth the clearness of the execution flow of the firs case, imo.

希望这会有所帮助.

这篇关于在 setter 中自订阅 PropertyChanged 或添加方法调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆