windows-phone-7 - 如何更改 WP7 应用程序的启动页面

标签 windows-phone-7 mvvm-light

我想根据是否有一些设置存储在独立存储中而有不同的起始页面。

但是我不知道处理这个问题的最佳实践在哪里。即,如果我在隔离存储中找到某些东西,我希望用户获得 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/

相关文章:

windows-phone-7 - WP7 Mp3MediaStreamSource 演示不起作用

windows-phone-7 - 如何显示项目资源中的图片?

c# - MVVMLight工具箱Messenger类引起问题。射击N次

c# - 从代码访问 MVVM Light ViewModelLocator

wpf - 我在哪里可以找到 Galasoft.MvvmLight.WPF45 程序集?

visual-studio-2010 - 未找到 System.data.linq 命名空间

c# - 在 WP7/WP8 中禁用图像透明部分的点击事件

c# - 如何替换 RichTextBox 中的超链接链接(Windows 手机)

visual-studio-2015 - MVVM Light 和 EventToCommand 在 VS2015 中给出无效标记

c# - MVVM 光 : Using DataService to retrieve database items