×
Namespaces

Variants
Actions
Revision as of 09:30, 15 October 2013 by hamishwillee (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

How to dynamically set the application start page on Windows Phone

From Nokia Developer Wiki
Jump to: navigation, search

This article explains how to dynamically set the default (start) page of application in Windows Phone.

WP Metro Icon UI.png
SignpostIcon XAML 40.png
WP Metro Icon WP8.png
SignpostIcon WP7 70px.png
Article Metadata
Tested with
SDK: Windows Phone 8.0 SDK
CompatibilityArticle
Created: munjalrohit (16 Sep 2013)
Last edited: hamishwillee (15 Oct 2013)

Contents

Introduction

The default start page for an application is set in WMAppManifest.xml - in the DefaultTask node of the NavigationPage tag. While having a static start page is ideal for some apps, there are many cases where it is useful to be able to dynamically select the start page.

For example, it is often useful to have:

  • a different start page for when user login credentials are stored from when they are not stored
  • a "wizard" start sequence for the first time the user activates the app


This article shows how to change the remove the default page and dynamically set the new start page in C# code. It uses the context of an app which has different screens for logging in (Login.xaml) and for when they have already entered their details (HomeScreen.xaml).

Note.pngNote: It is also possible to have a default page which dynamically configures itself. While this may be suitable in many cases, usually it is better for code maintenance to have separate pages.


Removing the default page

The first step is to remove the default page that us set by default in the manifest file (WMAppManifest.xml) in apps created from the standard templates.

Simply remove NavigationPage="MainPage.xaml" from the code below.

<Tasks>
<DefaultTask Name="_default" NavigationPage="MainPage.xaml" />
</Tasks>

Set the start page after the app has initialised

The start page is specified in InitializePhoneApplication() by calling RootFrame.Navigate() (after other initialisation has completed).

RootFrame.Navigate(new Uri("/YourStartPage.xaml", UriKind.RelativeOrAbsolute));

The example below shows a different start page being selected based on a check of the application settings to determine if login information has already been stored (Login status is checked with IsolatedStorageSettings.ApplicationSettings.Contains("islogin").)

  private void InitializePhoneApplication()
{
if (phoneApplicationInitialized)
return;
 
// Create the frame but don't set it as RootVisual yet; this allows the splash
// screen to remain active until the application is ready to render.
RootFrame = new PhoneApplicationFrame();
RootFrame.Navigated += CompleteInitializePhoneApplication;
 
// Handle navigation failures
RootFrame.NavigationFailed += RootFrame_NavigationFailed;
 
// Handle reset requests for clearing the backstack
RootFrame.Navigated += CheckForResetNavigation;
 
// Ensure we don't initialize again
phoneApplicationInitialized = true;
 
Uri uri;
if (IsolatedStorageSettings.ApplicationSettings.Contains("islogin"))
{
if (!(Convert.ToString(IsolatedStorageSettings.ApplicationSettings["islogin"]).ToLower() == "yes"))
{
RootFrame.Navigate(new Uri("/LoginScreen.xaml", UriKind.RelativeOrAbsolute));
}
else
{
RootFrame.Navigate(new Uri("/HomeScreen.xaml", UriKind.RelativeOrAbsolute));
}
}
else
{
RootFrame.Navigate(new Uri("/LoginScreen.xaml", UriKind.RelativeOrAbsolute));
}
}

Summary

This article shows how to remove the default start page from XAML and set the new start page of an app dynamically.

This page was last modified on 15 October 2013, at 09:30.
436 page views in the last 30 days.