如何在WP7应用程序上更改启动页面

问题描述:

我希望有一个不同的起始页面,具体取决于是否在IsolatedStorage中存储了一些设置.

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

Bu我不知道在哪里处理该问题的最佳实践.也就是说,如果我在隔离的存储中找到了某些东西,我希望用户获得MainPage,否则我希望用户获得设置"页面.

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.

如果要使用一些神奇的东西,我正在使用MVVM-light.

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

Br

您可以通过将虚拟页面设置为项目的主页来做到这一点.您可以通过编辑项目的WMAppManifest.xml文件来更改主页:

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.

要执行此操作,请在App.xaml.cs文件中的构造函数结尾处,订阅"Navigating"事件:

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)));
    }
}

修改

实际上,这更容易.在应用程序构造函数的末尾,只需将UriMapper设置为您想要的替换Uri:

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;