.NET Tools
Essential productivity kit for .NET and game developers
INotifyPropertyChanged Support in ReSharper 7
Let’s see what’s good in ReSharper 7 in addition to support for Visual Studio 2012 and WinRT development.
Ever since the release of Windows Forms, the INotifyPropertyChanged
interface has been used by developers to allow properties to notify whoever needs to know (typically, the UI) that they have changed. Support for change notification, which nowadays appears in many core (WPF, Silverlight) and third-party frameworks, has been frequently requested on the ReSharper issue tracker and, in the 7.0 release, we are happy to oblige. Here’s what we’ve got in store.
Automatic implementation
As soon as you indicate that a class implements INotifyPropertyChanged
, ReSharper offers a quick-fix that not only implements the interface members, but also provides a default implementation of the method as well as the needed event.
With the interface implementation in place, you can navigate to a property and convert it to a field+property declaration that calls the notification method. The property can be an auto-property or a field-backed property provided the assignment to the backing field is the last expression in the setter.
If you have fields instead of properties, you can also invoke the Generate|Properties action, and ReSharper offers the option to generate properties matching those fields with the required change notifications:
Important note: in order to get context actions related to an implemented INotifyPropertyChanged
interface to show up, code annotations need to be added to the project. You can find the default implementation in ReSharper|Options|Code Annotations. These annotations contain the definition of the [NotifyPropertyChangedInvocator]
attribute, which is used to decorate the method that is responsible for firing the event. Also note that this is only necessary if you’re implementing INotifyPropertyChanged
yourself, and is not required if you are inheriting from a framework class that implements it.
Tracking Symbol Names
Referring to a property by name is not the safest way of doing things, but if you have to do it, ReSharper has got your back. First, ReSharper keeps a close eye on those property names for you. Should you make a mistake in a name or, for instance, refer to a property declared private
, ReSharper will complain about it:
In addition, when you decide to call OnPropertyChanged
manually, ReSharper will offer a completion list for that string parameter.
Framework Support
Not all implementations of INotifyPropertyChanged
are identical. Third-party application frameworks often have their preferred way of notifying on property changes, and are shipped with corresponding base classes that framework users are encouraged to inherit from. One major difference is that certain frameworks prefer to use a lambda expression instead of a string literal as a parameter to the notification method.
ReSharper 7 supports the following frameworks out of the box:
-
MVVM Light (WPF 3 and 4; Silverlight 3, 4 and 5; Windows Phone 7 and 7.1)
-
Caliburn.Micro (WPF 3 and 4; Silverlight 4 and 5; Windows Phone 7.1)
-
Prism (WPF 4; Silverlight 4)
-
Catel (WPF 3 and 4)
-
MVVMHelpers (WPF 3 and 4, only lambda expressions supported)
What this means in practical terms is that if you were to inherit from, say, Prism’s NotificationObject
, when it comes to generating properties you actually get a choice of which RaisePropertyChanged
method the implemented properties will call:
If we pick the lambda-driven implementation, our properties will be defined similar to the following:
And, of course, this choice will correspondingly be presented in the property-altering context actions:
As you can see, ReSharper is attuned to the specifics of the parents of the containing class, and is capable of presenting all of the available choices.