I am reading up iNotifyPropertyChanged in detail.
Can someone please clarify why do we need to check for
PropertyChanged !=null
?
Why would an event be null? Or in other words, why even check if it is null? The only time NotifyPropertyChanged
is called is when PropertyChanged
has been raised ( so it cannot be null), isn't it. Who/What can make it null?
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(string info)
{
if (PropertyChanged != null)
{
PropertyChanged(this,new PropertyChangedEventArgs(info));
}
}
Thank you.
The PropertyChanged event can indicate all properties on the object have changed by using either null or String. Empty as the property name in the PropertyChangedEventArgs. Note that in a UWP application, String.
The INotifyPropertyChanged interface is used to notify clients, typically binding clients, that a property value has changed. For example, consider a Person object with a property called FirstName .
To implement INotifyPropertyChanged you need to declare the PropertyChanged event and create the OnPropertyChanged method. Then for each property you want change notifications for, you call OnPropertyChanged whenever the property is updated.
The INotifyPropertyChanged changed interface is at the heart of XAML apps and has been a part of the . NET ecosystem since the early days of Windows Forms. The PropertyChanged event notifies the UI that a property in the binding source (usually the ViewModel) has changed. It allows the UI to update accordingly.
If nobody has subscribed to the event it will be null. So, you'd get a NullReferenceException on the event at runtime if you didn't.
In the case of the interface you're talking about, its also likely the raising action will occur before the subscriber has a chance to subscribe albeit imminent they are going to subscribe because the INotifyPropertyChanged interface is quite chatty.
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