user1290653
user1290653

Reputation: 775

Windows Phone Navigation - Passing back to the page before

I would like to click on a button to take me to a page , then click on a listbox item, click on a button on the new page and pass it back to the page before without creating a new URI of the first page.

        **First Page**
        private void btnAddExistingMember_Click(object sender, RoutedEventArgs e)
        {
              NavigationService.Navigate(new Uri("/ChooseMember.xaml", UriKind.Relative));
        }

        **Second page after choosing listbox value**
        private void btnAddSelected_Click(object sender, RoutedEventArgs e)
        {
              Member currMember = (Member)lstMembers.SelectedItem;
              string memberID = currMember.ID.ToString();
              //navigate back to first page here passing memberID
        }

can it be done?

Thank you

Upvotes: 3

Views: 1603

Answers (5)

Akshit Arora
Akshit Arora

Reputation: 458

I found a solution at codeproject which was quite useful to me.

While moving from the second form, save your data in PhoneApplicationService.Current.State

protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
{
    // Called when a page becomes the active page in a frame
    base.OnNavigatedFrom(e);
    // Text is param, you can define anything instead of Text 
    // but remember you need to further use same param.
    PhoneApplicationService.Current.State["Text"] = txtboxvalue.Text;
}

Go back using same NavigationService.GoBack(); and in OnNavigatedTo method, fetch the following code.

protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
    base.OnNavigatedTo(e);
    if (PhoneApplicationService.Current.State.ContainsKey("Text"))
    txtvalue.Text = (string)PhoneApplicationService.Current.State["Text"];
}

References:

MSDN: PhoneApplicationService Class

Original Solution at: How to pass values between pages in windows phone

Upvotes: 2

Sorokin Andrey
Sorokin Andrey

Reputation: 419

You can simply use

//first page
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
    string value = string.Empty;
    IDictionary<string, string> queryString = this.NavigationContext.QueryString;
    if (queryString.ContainsKey("memberID"))
    {
        memberID = queryString["memberID"];
        if (memberID != "-1")
            //your code here
    }
    base.OnNavigatedTo(e);
}

//second page
private void btnAddSelected_Click(object sender, RoutedEventArgs e)
{
    Member currMember = (Member)lstMembers.SelectedItem;
    string memberID = currMember.ID.ToString();

    string target = "/FirstPage.xaml";
    target += string.Format("?memberID={0}", memberID);
    NavigationService.Navigate(new Uri(target, UriKind.Relative));
}

Upvotes: 1

earthling
earthling

Reputation: 5264

Sounds to me like you want to set some object as the context for another page. Messaging in MVVM Light sounds like a good solution for this. Doesn't look like you're using MVVM so this may not be immediately applicable. This post pretty much lays out what I'm saying here.

Second Page
Create your SelectedObject property and make sure to call

RaisePropertyChanged(SelectedObjectPropertyName, oldValue, value, true);

The last parameter true says to broadcast this change in value to anyone listening. You'll need to wire up some other commands for listbox selected item and button click etc, but I won't go into that here since it's not directly related to your question. Selecting the Listbox item will simply set the data item for the first page like you want to accomplish. The button click can deal with the navigation.

First Page
In your view model constructor, register to receive the change broadcasted from Second Page

Messenger.Default.Register<PropertyChangedMessage<MyObject>>(this, (action) => UpdateObject(action.NewValue));

then define UpdateObject

private void UpdateObject(MyObject newObject)
{
    LocalObjectProperty = newObject;
}

Upvotes: 1

Paul Diston
Paul Diston

Reputation: 3294

You could create a manager class that would hold the member id. This manager class could then be accessed from both your first page and ChooseMember page.

An example of a Singleton Manager class :-

public class MyManager
{
    private static MyManager _instance;

    public static MyManager Instance
    {
        get
        {
            if (_instance == null)
            {
                _instance = new MyManager();
            }
            return _instance;
        }
    }
}

Upvotes: 2

TutuGeorge
TutuGeorge

Reputation: 2022

You can store the member in the App.xaml.cs file. This is common file accesssible for all files in the application. This works like a global variable.

//App.xaml.cs
int datafield ;

//Page1xaml.cs
(App.Current as App).dataField =10;

//Page2.xaml.cs
int x =  (App.Current as App).dataField 

Upvotes: 3

Related Questions