|
CodePlugIn_INotifyPropertyChanged_Property
DxCore Code Extension Plug In that implements the INotifyPropertyChanged interface pattern for properties.
Build using DXCore 11.1IntroductionThis code expansion plugin allows you to expand or convert a property declaration so that it also fires the PropertyChanged event of the INotifyPropertyChanged interface.
This plugin contains two CodeProviders:
For the lambda expression base class support, you can use the Prism 4 NotificationObject base class: http://msdn.microsoft.com/en-us/library/microsoft.practices.prism.viewmodel.notificationobject(v=pandp.39).aspx. UsagePlace the cursor anywhere in the property name, invoke refactoring (CTRL-~ default). Select either "Convert to INPC Property" or "Convert to INPC Property Base Class Call" from the Code extension operations context menu. DetailsFor initial code like this: public class Customer
{
public int CustomerId { get; set; }
}If you select "Convert to INPC Property", you will get: public class Customer : INotifyPropertyChanged
{
int _CustomerId;
public int CustomerId
{
get { return _CustomerId; }
set
{
if (value != _CustomerId)
{
_CustomerId = value;
PropertyChanged(this, new PropertyChangedEventArgs("CustomerId"));
}
}
}
public event PropertyChangedEventHandler PropertyChanged = delegate { };
}Note the expansion of the property with a backing store, the call to PropertyChanged event from the set block, the addition of the INotifyPropertyChanged interface to the class, and the declaration of the PropertyChanged to satisfy the interface. The code uses an anonymous method to initialize the event so that there is always an empty subscriber in the list, removing the need for null checking before firing the event. If instead you first add a base class that implements INPC and has an RaisePropertyChanged virtual method, such as the Prism NotificationObject class that supports a lambda expression pointer to the property: public class Customer : NotificationObject
{
public int CustomerId { get; set; }
}Then you invoke refactoring on the property and select "Convert to INPC Property Base Class Call", the result will be: public class Customer : NotificationObject
{
int _CustomerId;
public int CustomerId
{
get { return _CustomerId; }
set
{
if (value != _CustomerId)
{
_CustomerId = value;
RaisePropertyChanged(() => CustomerId);
}
}
}
}CreditsAuthor: Brian Noyes | |


