【问题标题】:How to change startup page on WP7 application如何更改 WP7 应用程序的启动页面
【发布时间】:2012-02-12 09:57:30
【问题描述】:

我希望有不同的起始页,这取决于是否有一些设置存储在隔离存储中。

但我不知道处理这个问题的最佳做法是在哪里。即,如果我在隔离存储中发现某些内容,我希望用户获取 MainPage,否则我希望用户获取 Settings-page。

如果有一些神奇的东西可以使用,我会使用 MVVM-light。

Br

【问题讨论】:

标签: windows-phone-7 mvvm-light


【解决方案1】:

您可以通过将虚拟页面设置为项目的主页来做到这一点。您可以通过编辑项目的 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;

【讨论】:

  • 您好,通过应用文件中的隔离存储进行操作是一种好习惯吗?
  • 只要不做冗长的操作应该没问题。访问应用程序文件中的隔离存储没有问题,但是您应该知道此代码在显示应用程序的第一页之前执行。因此,您只有 5 秒的时间进行计算,否则您的应用将无法通过认证。
  • Greate,tnx 获取有关 5 秒规则的信息 :) 你的第二个解决方案效果很好
猜你喜欢
  • 1970-01-01
  • 2011-06-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-11-20
  • 1970-01-01
相关资源
最近更新 更多