Metro Nuggets

Bitesized tidbits for building Modern (Metro) apps.

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; }
                if (value != progressText)
                    progressText = value;

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).

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

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.



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


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

Google photo


Twitter picture


Facebook photo


Connecting to %s

%d bloggers like this: