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

Car*_*ist 6 windows-phone-7 mvvm-light

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

我不知道处理这个问题的最佳做法在哪里.即如果我在隔离存储中找到某些内容我会让用户获得MainPage,否则我会像用户一样获取设置页面.

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

BR

Kev*_*sse 9

您可以通过将虚拟页面设置为项目的主页面来完成此操作.您可以通过编辑项目的WMAppManifest.xml文件来更改主页面:

<DefaultTask Name="_default" NavigationPage="DummyPage.xaml" />
Run Code Online (Sandbox Code Playgroud)

现在,检测指向虚拟页面的所有导航,并重定向到您想要的任何页面.

为此,在App.xaml.cs文件中,在构造函数的末尾,订阅"导航"事件:

this.RootFrame.Navigating += this.RootFrame_Navigating;
Run Code Online (Sandbox Code Playgroud)

在事件处理程序中,检测导航是否定向到虚拟页面,取消导航,并重定向到所需的页面:

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)));
    }
}
Run Code Online (Sandbox Code Playgroud)

编辑

实际上,更容易.在app构造函数的最后,只需使用您想要的替换Uri设置UriMapper:

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;
Run Code Online (Sandbox Code Playgroud)