WP7 中的墓碑问题,无法判断是否需要恢复或实例化/查询新数据 [英] problems with tombstoning in WP7, cannot tell if i need to restore or instantiate/query new data

查看:19
本文介绍了WP7 中的墓碑问题,无法判断是否需要恢复或实例化/查询新数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚在 WP7 中遇到了臭名昭著的墓碑问题.假设我有 3 个页面,FirstPage.xaml、SecondPage.xaml 和 ThirdPage.xaml.自然流程将是:

i've just run into the infamous tombstoning problem/issue in WP7. let's say i have 3 pages, FirstPage.xaml, SecondPage.xaml, and ThirdPage.xaml. the natural flow will be:

FirstPage.xaml -> SecondPage.xaml -> ThirdPage.xaml

FirstPage.xaml -> SecondPage.xaml -> ThirdPage.xaml

换句话说,

主页 -> 包含对象列表的页面 -> 显示前一页中一个对象的详细信息的页面

main page -> page with list of objects -> page displaying one object in detail from previous page

当我从 FirstPage.xaml 转到 SecondPage.xaml 时,我必须执行数据库查询以获取 SecondPage.xaml 中的列表.然后我需要从 SecondPage.xaml 转到一个 ThirdPage.xaml(在我从列表中选择一个 MyObject 之后).在这一点上,墓碑对我来说变得非常混乱.

when i go from FirstPage.xaml to SecondPage.xaml, i have to do a database query to get a List in SecondPage.xaml. i then need to go to a ThirdPage.xaml from SecondPage.xaml (after i select one MyObject from List). at this point, tombstoning is becoming very confusing for me.

我知道当进入 FirstPage.xaml -> SecondPage.xaml 时,调用 SecondPage.xaml.cs 的构造函数.我知道当去 ThirdPage.xaml -> SecondPage.xaml(返回,通过点击后退按钮或 NavigationService.GoBack())时,不会调用 SecondPage.xaml.cs 的构造函数.当我从 SecondPage.xaml 移动到 ThirdPage.xaml 时,我将视图模型 (VM) 对象存储在 PhoneApplicationService.Current.State (SecondPage.xaml.cs.OnPageNavigatedFrom()) 中.

i know when going FirstPage.xaml -> SecondPage.xaml, the constructor of SecondPage.xaml.cs is called. i know when going ThirdPage.xaml -> SecondPage.xaml (going back, by hitting the back button or NavigationService.GoBack()), the constructor of SecondPage.xaml.cs is NOT called. when i move from SecondPage.xaml to ThirdPage.xaml, i store the view-model (VM) objects in PhoneApplicationService.Current.State (SecondPage.xaml.cs.OnPageNavigatedFrom()).

我的(有缺陷的)策略是,如果在一个实例(FirstPage.xaml -> SecondPage.xaml)中调用 SecondPage.xaml.cs 的构造函数,而不是在另一个实例(ThirdPage.xaml -> SecondPage)中调用.xaml),然后我可以在构造函数中设置一个布尔标志是执行新的数据库查询还是恢复页面的状态(来自 PhoneApplication.Current.State).布尔标志最初设置为 false,并且仅在 SecondPage.xaml.cs 的构造函数中设置为 true.

my (flawed) strategy was, well, if the constructor of SecondPage.xaml.cs is called in one instance (FirstPage.xaml -> SecondPage.xaml), but not in the other instance (ThirdPage.xaml -> SecondPage.xaml), then i can set a boolean flag in the constructor whether to do a fresh DB query or to restore the page's state (from PhoneApplication.Current.State). the boolean flag is set to false initially, and only set to true in the constructor of SecondPage.xaml.cs.

我认为这很有效,但是当我按下开始按钮离开应用程序然后点击返回按钮返回应用程序时,SecondPage.xaml.cs 的构造函数被调用.所以我做了另一个新的数据库查询,而不是恢复状态,这不是预期的行为.

i thought this worked well, but then when i pressed the start button to leave the app and then hit the back button to come back to the app, the constructor of SecondPage.xaml.cs was called. so i do another fresh DB query instead of restoring the state, which is NOT the intended behavior.

我的问题是,当用户点击开始然后返回到应用程序时,我怎么知道什么时候进行新的数据库查询和恢复?我自己想过如何解决这个问题,但我想到的大部分都是杂七杂八的;这似乎不自然,好像我正在修补工作.例如,我认为我可以将查询字符串从 FirstPage.xaml 传递到 SecondPage.xaml(即/SecondPage.xaml?freshDbQuery=1),但是当我从 ThirdPage.xaml 移回 SecondPage.xaml 时,该查询字符串键值对,freshDbQuery=1,总是如此!(如你所知,我不太了解 wp7).

my question is this, how do i know when to do a fresh DB query vs a restore when the user hits start and then back to get to the app? i thought about how to solve this myself, but most of what i thought up were kludges; it seemed un-natural and as if i was tinkering to get things to work. for example, i thought i can pass in a querystring from FirstPage.xaml to SecondPage.xaml (i.e. /SecondPage.xaml?freshDbQuery=1), but when i move from ThirdPage.xaml back to SecondPage.xaml, that querystring key-value pair, freshDbQuery=1, is always so! (so as you can tell, i don't know wp7 too well).

感谢任何帮助.

推荐答案

所有对墓碑的处理都应该在 OnNavigatingFrom** 和 OnNavigatedTo 事件中完成.

All your handling for tombstoning should be done in the OnNavigatingFrom** and the OnNavigatedTo events.

您可以使用以下内容为您的情况创建通用处理程序:

You can create all purpose handlers for your situation with the following:

protected override void OnNavigatingFrom(NavigatingCancelEventArgs e)
{
    if (e.NavigationMode != System.Windows.Navigation.NavigationMode.Back)
    {
        this.State.Clear();
        this.State.Add("db_data", ***Serialized version of the DB returned data***);
    }

    base.OnNavigatingFrom(e);
}

protected override void OnNavigatedTo(NavigationEventArgs e)
{
    if (this.State.ContainsKey("db_data"))
    {
        this.SomethingOnPage = DeserializeToAppropriateType(this.State["db_data"]);
    }

    base.OnNavigatedTo(e);
}

** 尽可能优先于 OnNavigatedFrom 使用它.

** Use this in preference to OnNavigatedFrom wherever possible.

这篇关于WP7 中的墓碑问题,无法判断是否需要恢复或实例化/查询新数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆