Windows Phone 8.1 - Page Navigation
Asked Answered
A

4

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.

Apeman answered 18/4, 2014 at 12:39 Comment(4)
Are you getting any errors?Whitaker
this codes work for me on my nokia 630 with cyanJagatai
Plese refer to [this][1] answer, maybe it could help you... [1]: https://mcmap.net/q/426035/-windows-phone-8-1-navigate-using-string-instead-of-a-typeLongbow
NavigationService gives me errors.Circumvent
S
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

Solifidian answered 18/4, 2014 at 12:45 Comment(6)
And If I want to go back, clicking the hardware back button doesn't work so would I have to add the hardware back button pressed event and then enter the page which I want? or is their an easier way?Apeman
You can use NavigationHelper in your SecondPage, so that you don't need to add event handler.For example, you can create a BasicPage instead of BlankPage as your SecondPage. And you will see it.Solifidian
wp8.1 is different from wp8.0, but more similar with winrt.Solifidian
How To pass multiple Parameters.Cooler
@Devi Prasad You should define a class which contains your multiple parameters.Solifidian
This method worked for me in forward navigation. I added it to the click event in xmal.cs file.Circumvent
E
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.

Ecto answered 15/5, 2014 at 5:21 Comment(1)
...and don´t forget you can do it at app level: #24336425Longbow
V
1
// Navigation Without parameters

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



// Navigation with parameters

this.Frame.Navigate(typeof(SecondPage),MyParameters);
Vespertilionine answered 22/2, 2015 at 15:41 Comment(2)
How to pass Multiple ParaetersCooler
Use array or List<object> , put your parameters in it, and pass the array or the List<object> in the parameter. Do you want a sample ?Vespertilionine
B
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 = "[email protected]";

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.

Billybillycock answered 9/11, 2016 at 14:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.