在Windows Phone 8中更改UriMapper后重定向页面

5

我有一个应用程序,当RootFrame首次初始化时,我让它检查是否是应用程序首次启动。如果是,它将RootFrame UriMapper更改为教程页面。问题是,我似乎找不到一种方法将用户重定向回MainPage.xaml。到目前为止,它什么也没做。

这是我在App构造函数中用于更改初始启动页面的代码:

if (App.Model.SelectFirstStart())
{
    var mapper = new UriMapper();

    mapper.UriMappings.Add(new UriMapping
    {
       Uri = new Uri("/MainPage.xaml", UriKind.Relative),
       MappedUri = new Uri("/TutorialPage.xaml", UriKind.Relative)
       });

       RootFrame.UriMapper = mapper;
    }

当用户在教程页面点击按钮时,应将其重定向到MainPage.xaml页面。这是我迄今为止尝试过的内容: ``` 当用户在教程页面点击按钮时,应该将他们重定向到MainPage.xaml。这是我目前为止所尝试的: ```
NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));

并且:

App.RootFrame.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));

任何帮助都将不胜感激。谢谢。
1个回答

6
我看到了几个问题。
首先,您没有在教程页面中更改映射,因此,MainPage.xaml 仍将映射到 TutorialPage.xaml
第二个问题是即使在修复后,导航到 MainPage.xaml 仍然无法工作,因为尽管实际页面是 TutorialPage.xaml,但不管怎样,RootFrame.CurrentSource 都将指向 MainPage.xaml
要解决此问题,您需要在教程页面的按钮单击中执行以下操作。
// Inside the Button click event of TutorialPage.xaml
// Change the mapping so that MainPage points to itself
((UriMapper)App.RootFrame.UriMapper).UriMappings[0].MappedUri = 
    new Uri("/MainPage.xaml", UriKind.Relative);

// Since RootFrame.CurrentSource is still set to MainPage, you need to pass
// some dummy query string to force the navigation
App.RootFrame.Navigate(new Uri("/MainPage.xaml?dummy=1", UriKind.Relative));

// Remove back entry so that if user taps the back button 
// you won't get back to tutorial
App.RootFrame.RemoveBackEntry();

搞定了,非常感谢。我之前尝试重新映射它,但没有意识到我必须删除后面的条目。再次感谢! - user1186173

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接