Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redirect page after changing UriMapper in Windows Phone 8

I have an app, and when the RootFrame is first initialized, I have it checking if its the first time the app has been launched. If it is, it changes the RootFrame UriMapper to a tutorial page. the problem is, I can't seem to figure out a way to redirect the user back to MainPage.xaml. It just won't do anything so far.

This is the code I'm using for changing the initial start up page in the App constructor:

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;
    }

When the user taps a button in the tutorial page, it should redirect them to the MainPage.xaml. This is what I've tried so far:

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

And:

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

Any help would be much appreciated. Thank you

like image 298
user1186173 Avatar asked Jun 16 '13 16:06

user1186173


1 Answers

I see a couple of issues.

First, you are not changing the mapping in your tutorial page so basically MainPage.xaml still maps to TutorialPage.xaml.

Second issue is even after fixing that, navigation to MainPage.xaml will still not work because despite the actual page being TutorialPage.xaml, RootFrame.CurrentSource will still point to MainPage.xaml no matter what.

To fix this you need to do the following in your tutorial page's button click

// 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();
like image 128
Alaa Masoud Avatar answered Oct 20 '22 00:10

Alaa Masoud