Changing MainPage.xaml in a Windows Phone application

Last week I was trying to add some launch page logic to a windows phone application (*word dice* cough cough) where users will see the ‘help page’ the very first time they launch the application and the homepage on every other launch. If there’s any iOS developers out there you would know that is simple enough to do since you just have to initialize and push a different ViewController. However in windows phone, that logic is ‘abstracted out’ into the WMAppManifest.xml.

For person who’s new to windows phone it is tempting to rename “MainPage.xaml” and its code behind to sometime more meaningful, however if you do only that you will be stuck between a compiler and a hard place with the following run time exception:

No XAML was found at the location ‘/MainPage.xaml’

This happens because Windows phone applications store the first page to display ‘logic’ in the WMAppManifest.xml file.

But what if you wanted to add some custom logic?… Like for example show a login screen or profile page based on if they are logged in. I recently came across this issue and was disappointed to find that the top few hits in my search results suggested hacking the Navigation stack (and breaking the ‘back button’ experience). But eventually I discovered that you can do it by first blanking out the NavigationPage attribute in your manifest file:

Then in your App.xaml.cs add the following logic to the Application_Launching(…) event:

Simple isn’t it! Moral of the story? Blogs and forums aren’t always right. You should always keep looking even if super_nerd_1988 says you have no hope.

Leave a Reply