我想根据是否有一些设置存储在独立存储中而有不同的起始页面。
但是我不知道处理这个问题的最佳实践在哪里。即,如果我在隔离存储中找到某些东西,我希望用户获得 MainPage,否则我希望用户获得 Settings-page。
如果有一些神奇的东西可以使用,我正在使用 MVVM-light。
溴
最佳答案
您可以通过将虚拟页面设置为项目的主页来实现此目的。您可以通过编辑项目的 WMAppManifest.xml 文件来更改主页:
<DefaultTask Name="_default" NavigationPage="DummyPage.xaml" />
现在,检测指向虚拟页面的所有导航,并重定向到您想要的任何页面。
为此,在 App.xaml.cs 文件中,在构造函数的末尾,订阅“导航”事件:
this.RootFrame.Navigating += this.RootFrame_Navigating;
在事件处理程序中,检测导航是否定向到虚拟页面,取消导航,重定向到你想要的页面:
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)));
}
}
编辑
其实还有更简单的。在应用程序构造函数的末尾,只需使用您想要的替换 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;
关于windows-phone-7 - 如何更改 WP7 应用程序的启动页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9247908/