INotifyPropertyChanged Support in ReSharper 7
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.
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.
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.
Subscribe to Blog updates
Thanks, we've got you!
Eager, Lazy and Explicit Loading with Entity Framework Core
Entity Framework Core (EF Core) supports a number of ways to load related data. There’s eager loading, lazy loading, and explicit loading. Each of these approaches have their own advantages and drawbacks. In this post, let’s have a quick look at each of these ways to load data for navigational prope…
OSS Power-Ups: bUnit – Webinar Recording
The recording of our webinar, OSS Power-Ups: bUnit, with Egil Hansen and Steven Giesel, is available. This was the twelfth episode of our OSS Power-Ups series, where we put a spotlight on open-source .NET projects. Subscribe to our community newsletter to receive notifications about future webi…
Accelerating Your Testing Workflow with Unit Test Creation and Navigation
Unit tests play an important role in our daily development workflow. They help us ensure our codebase's correctness when writing new functionality or performing refactorings to improve readability and maintainability. In the process, we often create new test files that accompany the p…
Introducing Predictive Debugging: A Game-Changing Look into the Future
With the introduction of debugging tools, software developers were empowered to interactively investigate the control flow of software programs to find bugs in live environments. At JetBrains, we've always strived to improve the art of debugging. Besides the more standard things you expect from a de…