Windows Phone Navigation - Passing back to the pag

2019-02-18 20:03发布

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

5条回答
Deceive 欺骗
2楼-- · 2019-02-18 20:11

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;
        }
    }
}
查看更多
劫难
3楼-- · 2019-02-18 20:24

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

查看更多
我想做一个坏孩纸
4楼-- · 2019-02-18 20:32

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 
查看更多
闹够了就滚
5楼-- · 2019-02-18 20:34

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;
}
查看更多
Deceive 欺骗
6楼-- · 2019-02-18 20:38

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));
}
查看更多
登录 后发表回答