Implementing INotifyPropertyChanged - does a better way exist?

前端 未结 30 2729
感情败类
感情败类 2020-11-21 05:23

Microsoft should have implemented something snappy for INotifyPropertyChanged, like in the automatic properties, just specify {get; set; notify;} I

30条回答
  •  猫巷女王i
    2020-11-21 05:40

    I have just found ActiveSharp - Automatic INotifyPropertyChanged, I have yet to use it, but it looks good.

    To quote from it's web site...


    Send property change notifications without specifying property name as a string.

    Instead, write properties like this:

    public int Foo
    {
        get { return _foo; }
        set { SetValue(ref _foo, value); }  // <-- no property name here
    }
    

    Note that there is no need to include the name of the property as a string. ActiveSharp reliably and correctly figures that out for itself. It works based on the fact that your property implementation passes the backing field (_foo) by ref. (ActiveSharp uses that "by ref" call to identify which backing field was passed, and from the field it identifies the property).

提交回复
热议问题