37

Coming from Windows Phone 8 I have never thought there will be a lot of changes done to the Windows Phone 8.1 code. Basically I'm just wondering how to do page navigation just like how you would do it on Windows Phone 8. To do that you should add:

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

but that code doesn't work for Windows Phone 8.1.

Can someone please help me with this? If possible provide any links or documentation on all the new Windows Phone 8.1 methods.

gotqn
  • 36,464
  • 39
  • 145
  • 218
Ahmed.C
  • 487
  • 1
  • 6
  • 17

4 Answers4

63

In Windows Phone 8.1, Page Navigation method is like this:

Frame.Navigate(typeof(SecondPage), param);

It means that you will navagate to 'SecondPage', and pass 'param' (a class based on object).

If you needn't to pass any parameters, You can use this:

Frame.Navigate(typeof(SecondPage));

You can find the documentation for this MSDN link

Chris Shao
  • 7,991
  • 3
  • 36
  • 37
23

In case you want to go back you can use:

if(this.Frame.CanGoBack)
{
this.Frame.GoBack();
}

If you want to go back on the click of back button, you need to override the hardwarebutton event:

HardwareButtons.BackPressed += HardwareButtons_BackPressed;

void HardwareButtons_BackPressed(object sender, BackPressedEventArgs e)
        {
            Frame rootFrame = Window.Current.Content as Frame;
            if(rootFrame != null && rootFrame.CanGoBack)
            {
                rootFrame.GoBack();
                e.Handled = true;
            }

        }

Make sure to set e.Handled to true.

Harsha Bhat
  • 648
  • 7
  • 25
  • 2
    ...and don´t forget you can do it at app level: http://stackoverflow.com/questions/24335925/windows-phone-8-1-universal-app-terminates-on-navigating-back-from-second-page – Cabuxa.Mapache Jan 19 '15 at 15:07
1
// Navigation Without parameters

this.Frame.Navigate(typeof(SecondPage));



// Navigation with parameters

this.Frame.Navigate(typeof(SecondPage),MyParameters);
Rashad
  • 10,519
  • 4
  • 40
  • 67
Abdullah El-Menawy
  • 378
  • 1
  • 5
  • 16
0

To send multiple parameters: Its quite late to answer but might help someone. You can create a custom class, set your parameters in it and send its object as a parameter to your target page.

For example. Your custom class:

public class CustomDataClass
{
public string name;
public string email;
} 

CustomDataClass myData = new CustomDataClass();
myData.name = "abc";
myData.email = "abc@hotmail.com";

Frame.Navigate(typeof(SecondPage), myData);

And then on the target page you can retrieve in OnNavigatedTo function like this:

protected override void OnNavigatedTo(NavigationEventArgs e)
{
CustomDataClass myData2 = e.Parameter as CustomDataClass;
string name = myData2.name;
string email = myData2.email;
}

Hope it helps.

user5434084
  • 139
  • 1
  • 8