Windows Phone Navigation - Passing back to the page before

天大地大妈咪最大 提交于 2019-12-19 09:46:44

问题


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


回答1:


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 



回答2:


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:


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:


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;
}



回答5:


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));
}


来源:https://stackoverflow.com/questions/10316511/windows-phone-navigation-passing-back-to-the-page-before

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!