I have a hard time understanding the difference between those 3 things when creating a project using Xamarin.Forms or MAUI. I know what these are - INotifyPropertyChanged
is an interface you need to implement when you want to use bindings in XAML, BindableObject
is Xamarin.Forms class that implements said interface and ObservableObject
is a class found in Xamarin Community Toolkit that also implements that interface. I just don't really understand the differences between them (especially the latter two) and when you would use any of them? I have read different tutorials and they all say different things - that you need to implement the interface yourself (so your viewmodel implements it), that your viewmodel needs to inherit from BindableObject
, or - if you're using Xamarin Community Toolkit (which you probably should use/are using) - inherit from ObservableObject
. But - like I said - why should you use any of the solutions mentioned above over the others?
If you implement INotifyPropertyChanged
interface Visual Studio (or ReSharper extension, I'm not sure which one) automatically implements the method it needs to and adds this code:
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void PropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
But it makes your code longer/uglier, so I understand why you would want to only inherit from a class that already implements that interface. But then why some people say you should always implement it yourself? Is there a difference when you implement it yourself or inherit from a class that already implements it? Also, if you decide to inherit from a class - why use either one over the other one? The documentation of Xamarin.Forms is quite good in this regard, but it doesn't acknowledge the existence of Xamarin Community Toolkit and the Xamarin Community Toolkit documentation doesn't answer my question and doesn't say why you should use their ObservableObject
over Xamarin.Form's built-in BindableObject
.