2
votes

I have a Windows Phone 8.1 application.

On a page when the user hits the hardware back button I want to Navigate back but also want to send a parameter to the previous page. But GoBack function doesn't take any parameter. How do I pass a parameter back so that the previous page?

This is how I have handled Hardware back button currently

In my View Constructor

HardwareButtons.BackPressed += HardwareButtons_BackPressed;

The event handler

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

I would be very glad if someone can point me in the right direction. Thanks in Advance.

4
Why not just use PhoneApplicationService.Current.State to store all your values across your pages?Chubosaurus Software
@ChubosaurusSoftware That was supported only in Windows Phone Silverlight. Not supported in WinRT Universal Windows Phone 8.1 applications. People recommend to use Navigation parameter for the same. See here for more info stackoverflow.com/questions/23749975/…HelpMatters

4 Answers

3
votes

I solved this problem using singleton pattern where i created a controller class seen by all the mobile pages and through it i passes messages through them. Hope this answer helps you

1
votes

There are more than one way to handle this properly depending on the architecture of your app/project. Here are a couple of possible solutions:

1 - public (static) property object on App.xaml.cs. This way you can set the object from anywhere, and read it from anywhere.

public sealed partial class App : Application
{
    ............

    public static SomeType SomeObjectIPreferToShareAcrossApp { get; set; }

    ............
}

Then there's your backpressed handler:

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

Then when you navigate to a page, check if that object is set, read it, and reset it.

protected override void OnNavigatedTo(NavigationEventArgs e)
{
    if (e.NavigationMode == NavigationMode.Back)
    {
        if (App.SomeObjectIPreferToShareAcrossApp != null)
        {
            // yay, do something :)
            App.SomeObjectIPreferToShareAcrossApp = null;
        }
    }
}

I'm sure some people would object to this way of sharing because it may be error-prone and ugly.

2 - Some sort of event aggregator implementation, meaning that you publish/send a certain message or an object from a publisher to all of the subscribers.

3 - Shared ViewModel instances

There are other ways, but again - it depends on the architecture of your project/app.

0
votes

Create a class with something like:

  using System;

namespace SimpleMVVM.Model
{  
    public class Customer
    {
        public int CustomerID
        {
            get; 
            set;   
        }

        public string FullName
        {
            get;
            set;
        }

        public string Phone
        {
            get; 
            set;
        }
    }
}

And then you can assign/get these through your pages, rather than sending variables between pages.

0
votes

You must control to e.Handled like this:

        Frame rootFrame = Window.Current.Content as Frame;
        if (rootFrame != null && rootFrame.CanGoBack && !e.Handled)
        {
            rootFrame.GoBack();
            e.Handled = true;
        }

If you don't control to e.Handled, HardwareButtons.BackPressed event continue to back stack deep.