Navigation Strings in Windows Phone Apps

Whilst hacking away a few weekends ago at Hacked with the eminent Windows Phone developers Jay and Scott, I noticed Scott using a static constants class to store the navigation strings of pages in the app we were developing. I’d always hard-coded my navigations strings in the normal way, like so:

NavigationService.Navigate(new Uri("/MainPage.xaml", Urikind.Relative));

I’d always run into some difficulties when renaming or getting rid of a page in the app – bloody navigation exceptions.

So when I saw Scott using the constants in our hack I converted Foundbite to do the same and will be doing it this way in the future. Here’s how it works:

You have a static class to store your page relative strings for each page:

public static class WP8Constants
{
public const string MAIN_PAGE = "/MainPage.xaml";
public const string PROFILE_PAGE = "/ProfilePage.xaml";
public const string LARRY_PAGE = "/LarryPage.xaml";
public const string JIMMY_PAGE = "/JimmyPage.xaml";
public const string ELAINE_PAIGE = "/ElainePaige.xaml";
}

You have another static helper class to do the navigation for you:

public static class NavigationHelper
{
public static void Navigate(PhoneApplicationPage page, string navigateTo)
{
page.NavigationService.Navigate(new Uri(navigateTo, UriKind.Relative));
}
}

Then when you want to navigate to a page, it’s nice and easy:

NavigationHelper.Navigate(page, WP8Constants.LARRY_PAGE);

There’s less typing, Intellisense will give you your list of available pages AND should you want to interchange a page or change a page’s location you just need to change it your constants class.
Things like this make development just that little bit easier. 

Advertisements

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