简体   繁体   English

返回Windows Phone时,将数据传回上一页的最佳方法

[英]Best approach to pass data back to previous page when go back in Windows Phone

What would be the best approach to pass data back to the previous page when I click the back button in Windows Phone. 当我单击Windows Phone中的后退按钮时,将数据传递回上一页的最佳方法是什么。 What I want to do is, when the user goes back to the main page from one of my child pages, if I saved something in the child page, the main page should refresh de data. 我想要做的是,当用户从我的一个子页面返回主页面时,如果我在子页面中保存了某些内容,则主页面应刷新数据。 When I go forward is simple, I simply use the QueryStringm, but I don't know how to do when I go back. 当我前进很简单时,我只是使用QueryStringm,但是当我回去时我不知道该怎么做。 I tried this: 我试过这个:

private void PhoneApplicationPage_BackKeyPress(object sender, System.ComponentModel.CancelEventArgs e)
{
    e.Cancel = true;
    NavigationContext.QueryString.Add("test", "test");
}

and then, in the main page I tried to do this: 然后,在主页面中我尝试这样做:

protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
    if (e.NavigationMode == System.Windows.Navigation.NavigationMode.Back)
    {
        if (NavigationContext.QueryString.ContainsKey("test"))
        {
            MessageBox.Show(NavigationContext.QueryString["test"]);
        }
    }
}

I did this just for testing purposes, but I had no luck, I've searched the internet for an answer but I didn't find any satisfying one. 我这样做仅仅是为了测试目的,但我没有运气,我在互联网上搜索答案,但我没有找到任何令人满意的答案。 I thought about putting in the App.xaml.cs a dictionary called Pending, where I would hold data that should be handled at certain points, and then, if I went back to the main page and I had the data in this dictionary I would refresh the ui. 我想在App.xaml.cs中添加一个名为Pending的字典,在那里我将保存应该在某些点处理的数据,然后,如果我回到主页并且我在这个字典中有数据,我会刷新ui。 Would it be a good idea?? 这是个好主意吗?

Thank you all! 谢谢你们!

What I would recommend is if you are following the MVVM architecture, set a property in the view model from child page and access it on the parent page. 我建议如果您遵循MVVM架构,请从子页面在视图模型中设置属性并在父页面上访问它。 I hope it helps you 我希望它对你有所帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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