调用 INotifyPropertyChanged 的​​ PropertyChanged 事件的最佳方法是什么?

发布于 2024-07-12 00:19:55 字数 1100 浏览 16 评论 0原文

当您实现 INotifyPropertyChanged 接口时,您负责在类中每次更新属性时调用 PropertyChanged 事件。

这通常会产生以下代码:

    public class MyClass: INotifyPropertyChanged

        private bool myfield;
        public bool MyField
        {
            get { return myfield; }
            set
            {
                if (myfield == value)
                    return;
                myfield = value;
                OnPropertyChanged(new PropertyChangedEventArgs("MyField"));
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        private void OnPropertyChanged(PropertyChangedEventArgs e)
        {
            PropertyChangedEventHandler h = PropertyChanged;
            if (h != null)
                h(this, e);
        }
   }

即每个属性 12 行

如果能够像这样装饰自动属性,那就简单多了:

[INotifyProperty]
public double MyField{ get; set; }

但不幸的是,这是不可能的(请参阅 msdn 上的这篇文章

如何减少每个属性所需的代码量?

When you implement the INotifyPropertyChanged interface, you're responsible for calling the PropertyChanged event each and everytime a property is updated in the class.

This typically leads to the following code :

    public class MyClass: INotifyPropertyChanged

        private bool myfield;
        public bool MyField
        {
            get { return myfield; }
            set
            {
                if (myfield == value)
                    return;
                myfield = value;
                OnPropertyChanged(new PropertyChangedEventArgs("MyField"));
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        private void OnPropertyChanged(PropertyChangedEventArgs e)
        {
            PropertyChangedEventHandler h = PropertyChanged;
            if (h != null)
                h(this, e);
        }
   }

That is 12 lines per property.

It would be so much simpler if one was able to decorate automatic properties like this :

[INotifyProperty]
public double MyField{ get; set; }

But unfortunately this is not possible (see this post on msdn for example)

How can I reduce the amount of code needed per property?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

年少掌心 2024-07-19 00:19:55

实际上,每个属性只有 3-4 行; 其他行在所有“通知”属性上摊销

class Person : INotifyPropertyChanged
{
    #region INotifyPropertyChanged: Shared bit
    public event PropertyChangedEventHandler PropertyChanged;

    private void OnPropertyChanged(PropertyChangedEventArgs e)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, e);
    }
    #endregion

    private string _firstName;
    public string FirstName
    {
        get { return _firstName; }
        set
        {
            if (_firstName == value)
                return;
            _firstName = value;
            OnPropertyChanged(new PropertyChangedEventArgs("FirstName"));
        }
    }

    // Ditto for other properties
}

您可以尝试类似以下的操作,它分担更多负载:

private string _firstName;
public string FirstName
{
    get { return _firstName; }
    set { SetNotifyingProperty("FirstName", ref _firstName, value); }
}
private void SetNotifyingProperty<T>(string propertyName,
                                     ref T field, T value)
{
    if (value.Equals(field))
        return;
    field = value;
    OnPropertyChanged(new PropertyChangedEventArgs(propertyName));
}

Actually, it's only 3-4 lines per property; the other lines are amortized over all "notifying" properties:

class Person : INotifyPropertyChanged
{
    #region INotifyPropertyChanged: Shared bit
    public event PropertyChangedEventHandler PropertyChanged;

    private void OnPropertyChanged(PropertyChangedEventArgs e)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, e);
    }
    #endregion

    private string _firstName;
    public string FirstName
    {
        get { return _firstName; }
        set
        {
            if (_firstName == value)
                return;
            _firstName = value;
            OnPropertyChanged(new PropertyChangedEventArgs("FirstName"));
        }
    }

    // Ditto for other properties
}

You could try something like the following, which shares some more of the load:

private string _firstName;
public string FirstName
{
    get { return _firstName; }
    set { SetNotifyingProperty("FirstName", ref _firstName, value); }
}
private void SetNotifyingProperty<T>(string propertyName,
                                     ref T field, T value)
{
    if (value.Equals(field))
        return;
    field = value;
    OnPropertyChanged(new PropertyChangedEventArgs(propertyName));
}
心是晴朗的。 2024-07-19 00:19:55

我现在要做的就是在我的类中编写这个:

 //AUTOGENERATE INotifyProperty
 private bool myfield;

我编写了一个小工具,可以在部分类中生成所有需要的属性代码。
这绝不是一个优雅的解决方案,但它确实有效:)

What I do for now is that I write this in my class :

 //AUTOGENERATE INotifyProperty
 private bool myfield;

And I wrote a small tool which generate all the needed property code in a partial class.
This is by no way an elegant solution, but it works :)

怎会甘心 2024-07-19 00:19:55

外包是最有意义的。

使用以下代码编写一个类(ObservableObject):

class ObservableObject : INotifyPropertyChanged
{
  public event PropertyChangedEventHandler PropertyChanged;

  protected void OnPropertyChanged(string name)
  {
    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
  }
}

所有从该类派生的类都可以访问该方法,这要归功于 protected。

例子:

class Example : ObservableObject
{
  //propfull
  private string name;
  public string Name
  {
    get {return name;}
    set 
    {
      name = value;
      OnPropertyChanged(nameof(Name));
    }
  }
}

It makes most sense to outsource.

Write a class ( ObservableObject) with the following code:

class ObservableObject : INotifyPropertyChanged
{
  public event PropertyChangedEventHandler PropertyChanged;

  protected void OnPropertyChanged(string name)
  {
    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
  }
}

All classes derived from this class can access the method, thanks to protected.

Example:

class Example : ObservableObject
{
  //propfull
  private string name;
  public string Name
  {
    get {return name;}
    set 
    {
      name = value;
      OnPropertyChanged(nameof(Name));
    }
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文