Windows Template Studio — Simplify Properties with Fody
In this short article, I add Fody to make properties a little simpler.
What is Fody?
Specifically I am talking about the PropertyChanged.Fody NuGet package. This handy little package does one simple thing: it helps standard properties implement the PropertyChanged event.
So something this …
private SampleOrder _item;
public SampleOrder Item
{
get { return _item; }
set { Set(ref _item, value); }
}
Can be written like this …
public SampleOrder Item { get; set; }
And it still notifies data bound XAML that a change has occurred!
Adding Fody to the Code Behind Design Pattern
- Add the Fody NuGet package to the app project.
- Add the PropertyChanged.Fody NuGet package to the app project.
- Remove the Set and OnPropertyChanged methods from your code behind on your pages. Do not remove the PropertyChanged event.
- Update properties on the pages that were using the old method to simple properties with getters and setters.
Adding Fody to the MVVM Basic Design Pattern
- Add the Fody NuGet package to the app project.
- Add the PropertyChanged.Fody NuGet package to the app project.
- Update any class that is using the Observable Set method to use a simple getter and setter.
You can leave the Set method in place in case you need to add a more complex update scenario like throwing an event when a property is updated.
The other three MVVM frameworks available in Windows Template Studio are also listed as supported by Fody, these follow essential the same steps.
That’s it!