Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change startup page on WP7 application

I want to have different start-page depending on if there is some settings stored in IsolatedStorage.

Bu I don't know where the is the best practice to handle this. I.e if I find something in isolated storage I whant the user to get MainPage, otherwise I woluld like the user to get Settings-page.

I'm using MVVM-light if there is some magic stuff to use.

Br

like image 791
Carl-Otto Kjellkvist Avatar asked Feb 12 '12 09:02

Carl-Otto Kjellkvist


1 Answers

You can do this by setting a dummy page as the main page of your project. You can change the main page by editing the WMAppManifest.xml file of your project:

<DefaultTask Name="_default" NavigationPage="DummyPage.xaml" />

Now, detect all navigations directed to the dummy page, and redirect to whichever page you want.

To do this, in the App.xaml.cs file, at the end of the constructor, subscribe to the 'Navigating' event:

this.RootFrame.Navigating += this.RootFrame_Navigating;

In the event handler, detect if the navigation is directed to the dummy page, cancel the navigation, and redirect to the page you want:

void RootFrame_Navigating(object sender, NavigatingCancelEventArgs e)
{
    if (e.Uri.OriginalString == "/DummyPage.xaml")
    {
        e.Cancel = true;

        var navigationService = (NavigationService)sender;

        // Insert here your logic to load the destination page from the isolated storage
        string destinationPage = "/Page2.xaml";

        this.RootFrame.Dispatcher.BeginInvoke(() => navigationService.Navigate(new Uri(destinationPage, UriKind.Relative)));
    }
}

Edit

Actually, there's even easier. at the end of the app constructor, just set a UriMapper with the replacement Uri you want:

var mapper = new UriMapper();

mapper.UriMappings.Add(new UriMapping 
{ 
    Uri = new Uri("/DummyPage.xaml", UriKind.Relative),
    MappedUri = new Uri("/Page2.xaml", UriKind.Relative)
});

this.RootFrame.UriMapper = mapper;
like image 153
Kevin Gosse Avatar answered Sep 20 '22 12:09

Kevin Gosse