简体   繁体   中英

How to Pass Data Between Pages using OnNavigatedFrom and OnNavigatedTo Events

I have two pages in my application. The Navigation follows MainPage > EditPage > MainPage. MainPage is the starting page and edit page may be NavigatedTo upon a button click and NavigatedFrom using the hardware back button. If some action occurs in EditPage, I would like to inform MainPage when the user wishes to return to MainPage. I have referenced http://mobile.dzone.com/articles/passing-values-between-windows but I am not sure how fix the MainPage OnNavigatedTo event accordingly. SO far what I have is as follows

MainPage.xaml.cs

protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        //If action in EditPage occurred, determine that here
        ??
    }

EditPage.xaml.cs

protected override void OnNavigatedFrom(NavigationEventArgs e)
    {
        base.OnNavigatedFrom(e);

        //check to see if action occurred in EditPage, and if so, inform MainPage
        if (_wasEdited == true)
        {
            MainPage mp = e.Content as MainPage;
            if (mp != null)
                mp.Parameter = true;
        }
    }

You can have an App level variable, in your App.xaml.cs

public bool isThereAnyChange = false;

In your EditPage.xaml.cs, when ever you do a change, update 'isThereAnyChange'.

private void updateChangeStatus()
{
    (App.Current as App).isThereAnyChange = true;
}

Then in MainPage.xaml.cs chack for above variable.

protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        //If action in EditPage occurred, determine that here
        if((App.Current as App).isThereAnyChange)
              //The change is there do accordingly here.
    }

要在页面之间传递值,您可以使用querystring或应用程序状态变量,就区分导航而言,只需对NavigatedTo事件参数的e.URI属性进行检查,就可以了

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM