Windows Phone Navigation - Go to Previous Page

I would like to click on the button to go to the page, then click on the list item, click on the button on the new page and move it back to the page earlier without creating a new URI for 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 this be done?

thank

+3
source share
5 answers

You can save this item in the file App.xaml.cs. This is file sharing for all application files. It 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 
+3
source

You can create a manager class that will contain the identifier of the element. After that, this manager class can be obtained from both your first page and the ChooseMember page.

Singleton Manager: -

public class MyManager
{
    private static MyManager _instance;

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

codeproject, .

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

, NavigationService.GoBack(); OnNavigatedTo, .

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

:

MSDN: PhoneApplicationService Class

: Windows

+2

, - . MVVM Light . , MVVM, . , .


SelectedObject

RaisePropertyChanged(SelectedObjectPropertyName, oldValue, value, true);

true , . .., , . "" , . .


,

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

UpdateObject

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

//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));
}
+1

All Articles