开发者

INotifyPropertyChanged and Auto-Properties

Is there a way to use INotifyPropertyChanged with auto-properties? Maybe an attribute or something other, not obvious to me.

public string Demo{
    get;set;
}

For me, auto开发者_如何学运维-properties would be an extremely practicall thing, but almost always, I have to raise the PropertyChanged-event if the property value has been changed and without a mechanism to do this, auto-properties are useless for me.


In .NET 4.5 and higher it can be made somewhat shorter:

private int unitsInStock;
public int UnitsInStock
{
    get { return unitsInStock; }
    set { SetProperty(ref unitsInStock, value);}
}


It's something you would have to code yourself. The closest you could get would be something like this implementation on Code Project that uses a custom attribute and aspect orientated methods to give this syntax:

[NotifyPropertyChanged] 
public class AutoWiredSource
{ 
   public double MyProperty { get; set; } 
}

Someone once proposed on Microsoft Connect a change to the C# specification implement this:

class Person : INotifyPropertyChanged
{
    // "notify" is a context keyword, same as "get" and "set"
    public string Name { get; set; notify; }
}

But the proposal has been now closed.


There's no built-in mechanism to do this. Something like PostSharp would likely be able to add something like this for you (or Mark Gravell's HyperDescriptor, if you're just interested in making this databinding-aware).


INotifyPropertyChanged and DependencyProperties have certainly made properties a lot less fun. The best solution I've found is good snippets. Such as the ones that come in the Silverlight contrib project


I tried other ways first using:

  • BindableBase
  • DynamicViewModel

Those methods work, however I've still had to muddy the accessor methods of all properties in the Model or use a dynamic object which kills your auto-completion when coding. It's also worth mentioning that WinForms don't support binding to dynamic objects unfortunately.

Solution

Eventually I came across ReactiveUI.Fody. It's a simple to use solution using Fody and ReactiveUI. I used this in my WPF project successfully.

It weaves in the required boilerplate code, RaisePropertyChanges and ObservableAsPropertyHelper, at compile time.

Project Dependencies

https://github.com/kswoll/ReactiveUI.Fody

I installed the following packages in my project with NuGet:

<packages>
  <package id="Fody" version="2.0.7" targetFramework="net452" developmentDependency="true" />
  <package id="reactiveui" version="7.4.0" targetFramework="net452" />
  <package id="ReactiveUI.Fody" version="2.2.11" targetFramework="net452" />
  <package id="reactiveui-core" version="7.4.0" targetFramework="net452" />
  <package id="Rx-Core" version="2.2.5" targetFramework="net452" />
  <package id="Rx-Interfaces" version="2.2.5" targetFramework="net452" />
  <package id="Rx-Linq" version="2.2.5" targetFramework="net452" />
  <package id="Rx-Main" version="2.2.5" targetFramework="net452" />
  <package id="Rx-PlatformServices" version="2.2.5" targetFramework="net452" />
  <package id="Rx-XAML" version="2.2.5" targetFramework="net452" />
  <package id="Splat" version="1.6.0" targetFramework="net452" />
</packages>

Fody Setup

You'll need to create a FodyWeavers.xml file in the top level of you project so it looks like this:

<?xml version="1.0" encoding="utf-8" ?>
<Weavers>
  <ReactiveUI/>
</Weavers>

Usage

You just set your Model classes to inherit off ReactiveObject and add the [Reactive] attribute above any property you want to notify of changes.

using ReactiveUI;
using ReactiveUI.Fody.Helpers;

namespace name.domain.some{
    class SomeClass : ReactiveObject {
        [Reactive]
        public string SomeProperty { get; set; }
    }
}

Job done! Rebuild your project and check the output window in Visual Studio. You should see some lines outputted from Fody as it processes your annotated Model classes and injects the appropriate boilerplate code. It should look something like this:

INotifyPropertyChanged and Auto-Properties

Further Info

More useful info can be found here.


Useful answer by crea7or, ended up using it in my latest project. One way to improve it is by rewriting SetProperty this way:

protected bool SetProperty<T>(ref T storage, T value, string[] dependentPropertyNames = null, [CallerMemberName] string propertyName = null)
{
    if (Equals(storage, value))
    {
        return false;
    }

    storage = value;
    this.OnPropertyChanged(propertyName);

    if(dependentPropertyNames != null)
    {
        foreach(var dependentPropertyName in dependentPropertyNames)
        {
            OnPropertyChanged(dependentPropertyName);
        }
    }

    return true;
}

It now allows to pass in dependent property names allowing view to know that dependent property value has changed even though it was not set directly, but as a result of other properties being set.

Usage:

private string firstName;
public string FirstName
{
    get { return firstName; }
    set { SetProperty(ref firstName, value, new[] { "FullName" }); }
}

private string lastName;
public string LastName
{
    get { return lastName; }
    set { SetProperty(ref lastName, value, new[] { "FullName" }); }
}

public string FullName
{
    get { return $"{FirstName} {LastName}"; }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜