Notify Property Weaver extension for Visual Studio 2010 and Visual Studio 11

Notify Property Weaver extension by Simon Cropp automatically generates PropertyChanged notifications for your classes that implement INotifyPropertyChanged. With this extension you only need to declare properties, their implementation providing notifications on changes will be generated automatically in the binary assembly as a post build step.

A typical class with properties data changing over time can implement the INotifyPropertyChanged interface. When you bind such object, for example, to a WPF control, the control will display new content immediately after it become available. To illustrate this approach, let’s create a very simple application showing current time:

Simple clock application

Simple clock application

In a new WPF project, add a label to the main window:

<Label Content="{Binding Path=Time}" Height="28" HorizontalAlignment="Center" Name="label1" VerticalAlignment="Center" />

Set its DataContext to a Clock object:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        clock = new Clock();

        InitializeComponent();

        label1.DataContext = clock;
    }

    private Clock clock;
} 

Implement the Clock class with the dynamically updated Time property:

class Clock : System.ComponentModel.INotifyPropertyChanged
{
    public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;

    public Clock()
    {
        timer = new System.Windows.Threading.DispatcherTimer();
        timer.Interval = System.TimeSpan.FromSeconds(1);
        timer.Tick += new System.EventHandler(TimerTick);
        timer.Start();
    }

    public string Time
    {
        get
        {
            return time;
        }
        set
        {
            time = value;
            if (PropertyChanged != null)
                PropertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs("Time"));
        }
    }

    private void TimerTick(object sender, System.EventArgs e)
    {
        Time = System.DateTime.Now.ToLongTimeString();
    }

    private System.Windows.Threading.DispatcherTimer timer;
    private String time;
}

Now, if we had Notify Property Weaver installed, our Clock implementation could be simpler. Without definition for the Time property and without declaration of the time field:

class Clock : System.ComponentModel.INotifyPropertyChanged
{
    public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;

    public Clock() […]

    public string Time { get; set; }

    private void TimerTick(object sender, System.EventArgs e) […]

    private System.Windows.Threading.DispatcherTimer timer;
}

Of course, more dynamic properties you have, more code is saved. After you install Notify Property Weaver, you need to enable it for your project. Select the project in Solution Explorer and open Project – NotifyPropertyWeaver – Configure:

Notify Property Weaver configuration menu

Notify Property Weaver configuration menu


Notify Property Weaver configuration dialog

Notify Property Weaver configuration dialog

When you press OK in the configuration dialog, your project will be updated and after the next build omitted ProjectChanged notifications will be added to your assembly. You can verify it by running the application or by analyzing the disassembled code produced, for example, by dotPeek:

internal class Clock : INotifyPropertyChanged
{
private PropertyChangedEventHandler PropertyChanged;
private DispatcherTimer timer;

public string Time
{
    get
    {
    return this.Time__BackingField;
    }
    set
    {
    if (string.Equals(this.Time__BackingField, value))
        return;
    this.Time__BackingField = value;
    this.OnPropertyChanged("Time");
    }
}

public event PropertyChangedEventHandler PropertyChanged […]

public Clock() […]

private void TimerTick(object sender, EventArgs e) […]

public virtual void OnPropertyChanged(string propertyName)
{
    PropertyChangedEventHandler changedEventHandler = this.PropertyChanged;
    if (changedEventHandler == null)
    return;
    changedEventHandler((object) this, new PropertyChangedEventArgs(propertyName));
}
}

If you have a property that depends on other properties (e.g. DateTime depending on Time), Notify Property Weaver detects it and includes change notifications for this compound property as well:

public string DateTime { get {return "Today " + Time;} }
public string Time
{
    get
    {
    return this.Time__BackingField;
    }
    set
    {
    if (string.Equals(this.Time__BackingField, value))
        return;
    this.Time__BackingField = value;
    this.OnPropertyChanged("DateTime");
    this.OnPropertyChanged("Time");
    }
}

On the Behaviour tab of the configuration dialog you can adjust general options for the tool:

Notify Property Weaver behavior options

Notify Property Weaver behavior options

For example, if you enable the ProcessFields options, in your code you can change a property declaration to a shorter field declaration, preserving the functionality:

public string Time;

The free Notify Property Weaver extension supports .Net 3.5/4.0, Silverlight 3/4/5 and Windows Phone 7. Integrates with Visual Studio 2010 and Visual Studio 11. You can download the installer from Visual Studio Gallery. Source code for the extension is available on Google Code.

 

 

Organize Visual Studio tabs with Tabs Studio add-in

This entry was posted in Reviews and tagged , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s