How to change startup page on WP7 application

半世苍凉 提交于 2020-01-09 07:48:48

问题


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


回答1:


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;


来源:https://stackoverflow.com/questions/9247908/how-to-change-startup-page-on-wp7-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!