【发布时间】:2012-02-12 09:57:30
【问题描述】:
我希望有不同的起始页,这取决于是否有一些设置存储在隔离存储中。
但我不知道处理这个问题的最佳做法是在哪里。即,如果我在隔离存储中发现某些内容,我希望用户获取 MainPage,否则我希望用户获取 Settings-page。
如果有一些神奇的东西可以使用,我会使用 MVVM-light。
Br
【问题讨论】:
标签: windows-phone-7 mvvm-light
我希望有不同的起始页,这取决于是否有一些设置存储在隔离存储中。
但我不知道处理这个问题的最佳做法是在哪里。即,如果我在隔离存储中发现某些内容,我希望用户获取 MainPage,否则我希望用户获取 Settings-page。
如果有一些神奇的东西可以使用,我会使用 MVVM-light。
Br
【问题讨论】:
标签: windows-phone-7 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;
【讨论】: