问题
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