Metro Nuggets

Bitesized Windows Phone 7, Windows Phone 8 and Windows 8 tidbits

Windows Phone, Windows 8 and Fody

Those doing development for Windows Phone or Windows 8 and using an MVVM approach will be familiar with their ViewModels having properties that look something like this:

        private string progressText;
        public string ProgressText
        {
            get { return progressText; }
            set
            {
                if (value != progressText)
                {
                    progressText = value;
                    NotifyPropertyChanged("ProgressText");
                }
            }
        }

They will also be used to having lots of these in their ViewModels, too. It would be nice if we could just have the property look like this:

        public string ProgressText { get; set; }

Well, there is a way of doing it. The solution is a Visual Studio extension called Fody and its corresponding plugin to do this is called PropertyChanged. What Fody and its plugins do is inject code into your project at compile time.

It’s very easy to configure all this up, to start with, you need to install Fody.

In Visual Studio, go to Tools > Extensions and updates and do a search for Fody and install it (I should note here, Fody is compatible with both Visual Studio 2010 and 2012).
ExtensionManager

Now, installing the extension isn’t enough if you want to use it in your project. For each project you want this to be applied against, you need to enable it. To do that, select your project from the Solution explorer and go to Project > Fody > Enable.

On its own, Fody won’t do anything with your projects, this is where the multitude of plugins comes in, each one doing a different thing, and all can be found on nuget. For the case of this blog post, we are interested in the PropertyChanged plugin. With Fody now enabled, we need to add the PropertyChanged plugin to our project through nuget
PropertyChangedNuget

So how does Fody know when to inject the PropertyChanged code into our project? If you have a class that implements the INotifyPropertyChanged interface (or inherits from a class that does, like ViewModelBase in MVVMLight), then it will inject the code into that class’s properties.

What this then means is as a basic ViewModel, all you’d need is the following:

using System.ComponentModel;

namespace ScottIsAFool.ViewModel
{
    public class MyViewModel : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        public string PageTitle { get; set; }
    }
}

Simple, right? Now, I know what you’re thinking, what if you have to fire off another bit of code when the property value gets changed, how do you do that? Well, as it happens, that’s just as easy, all you need to do is create a method called OnPropertyNameChanged(). So in the case of the code above, your method would be OnPageTitleChanged(). That method call would then also get injected into the property’s setter at compile time.

So what does doing this give us? Well there are two main things:

  1. Saves an enormous amount of time because you just don’t need to write as much code
  2. Much more readable source code.

I highly recommend looking through the rest of the features of PropertyChanged (scroll to the bottom), and take a look at the rest of the Fody plugins. And if you’re not already using this in your projects, then I can’t recommend it enough. I don’t build a Windows Phone or Windows 8 app without it now.

SL

About these ads

3 responses to “Windows Phone, Windows 8 and Fody

  1. Pingback: Windows Phone, Windows 8 and Fody

  2. Pingback: Windows Phone, Windows 8 and Fody | Hispafoxing

  3. Austin January 16, 2013 at 07:23

    I don’t know anything regarding the back-end development of Windows 8, but also I have seen many are saying it awesome. Some of my friends are also working as Windows 8 application developer with Microsoft. It also started some student campaign.

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

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: