I am Using VS 2017 Community Edition I am creating MVVM pattern. After i installed fody i got error on my code while the instructor of the tutorial implemented it on vs 2015 here is the code:
using PropertyChanged;
using System.ComponentModel;
namespace GProject_MVVM.ViewModel
{
/// <summary>
/// A base view model that fires Property Changed events as needed
/// </summary>
[ImplementPropertyChanged] // **I got error here**
public class BaseViewModel : INotifyPropertyChanged
{
/// <summary>
/// The event that is fired when any child property changes its value
/// </summary>
public event PropertyChangedEventHandler PropertyChanged = (sender, e) => { };
/// <summary>
/// Call this to fire <see cref="PropertyChanged"/> event
/// </summary>
/// <param name="name"></param>
public void OnPropertyChanged(string name)
{
PropertyChanged(this, new PropertyChangedEventArgs(name));
}
}
}
[ImplementPropertyChanged] should not make error on this point the instructor implemented it successfully so is thier anything missing on my code ? The error says:
Severity Code Description Project File Line Suppression State Error CS0619 'ImplementPropertyChangedAttribute' is obsolete: 'This configuration option has been deprecated. The use of this attribute was to add INotifyPropertyChanged to a class with its associated event definition. After that all classes that implement INotifyPropertyChanged have their properties weaved, weather they have the ImplementPropertyChangedAttribute or not. This attribute was often incorrectly interpreted as an opt in approach to having properties weaved, which was never the intent nor how it ever operated. This attribute has been replaced by AddINotifyPropertyChangedInterfaceAttribute.' GProject_MVVM c:\users\ahmed hussainy\documents\visual studio 2017\Projects\GProject_MVVM\GProject_MVVM\ViewModel\BaseViewModel.cs 9 Active
The same code with Fody looks like this: The lines of code reduce dramatically and is clean from framework code. Under the hood Fody weaves the equivalence of what you see in the original file at compile time. Let's talk about how to set this up.
What is Fody and how it works. Fody is an extensible library for weaving .NET assembly written by Simon Cropp .It adds post build task in MS build pipeline to manipulate generated IL.
For the purpose of this blog I would demonstrate one very useful fody add-in called NullGaurd.As name suggests this add-in automatically adds code for raising exceptions when null value is encountered in a property or a method parameter.Below are steps for adding plug-in and using it. Create a sample project and add NullGaurd.Fody nuget package
Under the hood Fody weaves the equivalence of what you see in the original file at compile time. Let's talk about how to set this up. There are 3 simple steps. The first step is to add the nuget package. Next up, you'll need to create a FodyWeavers.xml file and add it to your project.
The exception already states the answer.
ImplementPropertyChangedAttribute' is obsolete: 'This configuration option has been deprecated. The use of this attribute was to add INotifyPropertyChanged to a class with its associated event definition. After that all classes that implement INotifyPropertyChanged have their properties weaved, weather they have the ImplementPropertyChangedAttribute or not.
With the new version of Fody.PropertyChanged you don't need to add the attribute any longer. Just make that class you want to be weaved implement INotifyPropertyChanged
and it will work.
So basically just remove / delete [ImplementPropertyChanged]
and it will compile and weave (if the weaver is present in FodyWeavers.xml
)
If you originally used this attribute exactly the way it was meant to be used, you should replace it with [AddINotifyPropertyChangedInterface]
.
This way Fody will add the INotifyPropertyChanged
interface to your class, and then the weaver will implement it properly.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With