Metro Nuggets

Bitesized tidbits for building Modern (Metro) apps.

Tag Archives: Tip

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.


ListBox to LongListSelector in Windows Phone 8

If you’re moving a project from Windows Phone 7 to Windows Phone 8 and you use ListBoxes in your app, you can keep them in there with no real problems. However, it’s not recommended to keep using them, instead use the LongListSelector instead. If you’re familiar with the LongListSelector from the Windows Phone Toolkit, then you’ll find the new native LongListSelector a very different beast and you probably wouldn’t think to use it because you know what it was previously only really used for. Previously you would only really use the LongListSelector if you were after a people hub or application list style grouping, but the new native version has been tweaked and improved upon and had massive performance enhancements. You can also just use it as a straight, flat ListBox, complete with all the performance enhancements it brings.

Making the change from ListBox to LongListSelector is actually very simple, just change your xaml to use the LongListSelector instead of the ListBox. It’s as simple as that. When you then run your app, it will already be a flat ListBox as you were before, but with those mentioned performance enhancements.


            <ListBox x:Name="LLS">
                        <TextBlock Text="{Binding}"
                                   Style="{StaticResource PhoneTextLargeStyle}" />


            <phone:LongListSelector x:Name="LLS">
                        <TextBlock Text="{Binding}"
                                   Style="{StaticResource PhoneTextLargeStyle}" />

It’s as simple as that.

Stay tuned for more on the new LongListSelector.


Test Your App With No Network

With Windows Phone 7, there was no way to be able to debug your app for when there might be no network connection. All you could do was Build > Deploy > Pray. With the release of the Windows Phone 8 SDK, that’s now changed.

Read more of this post

Marketplace Links, a Follow Up: There’s An App For That

A couple of weeks ago, I posted about the best way to share marketplace links, so that links would open in the correct marketplace for your platform (web or phone). But if someone is still sharing the web links, well there’s a lot of faffing around to get that to open in the marketplace on the phone, so is there a solution? Yes.

Read more of this post

Binding To Anonymous Types

I was working on an app recently where an xml datasource was pulled in and I decided to use linq to parse the data to an anonymous types. The plan was to then bind that data to the listbox in my UI. The data got parsed correctly and I could see my anonymous type, but my UI was simply not being updated. So what was wrong?

Read more of this post

Address Bar and Bing Search Bar Scrolling

One of the new things I found recently whilst trying to do something on my phone was the easy way in which someone can see all of what might be in the IE9 address bar or the Bing search bar. If you have a long link in IE9 it can be very tiresome to try and get back to the beginning (or the end) of the link from within the address bar itself; the same applies to the Bing search bar.

Read more of this post