WP7 中的墓碑问题,无法判断我是否需要恢复或实例化/查询新数据
我刚刚遇到了 WP7 中臭名昭著的墓碑问题。假设我有 3 个页面,FirstPage.xaml、SecondPage.xaml 和 ThirdPage.xaml。自然流程将是:
FirstPage.xaml -> SecondPage.xaml -> ThirdPage.xaml
换句话说,
主页 ->包含对象列表的页面 -> 当我从 FirstPage.xaml 转到 SecondPage.xaml 时,页面详细显示上一页中的一个对象
,我必须执行数据库查询才能获取 SecondPage.xaml 中的列表。然后,我需要从 SecondPage.xaml 转到 ThirdPage.xaml(在我从列表中选择一个 MyObject 之后)。在这一点上,墓碑对我来说变得非常混乱。
我知道什么时候去 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.OnPageNavieratedFrom()) 中。
我的(有缺陷的)策略是,如果在一个实例(FirstPage.xaml -> SecondPage.xaml)中调用 SecondPage.xaml.cs 的构造函数,但不在另一个实例(ThirdPage.xaml -> SecondPage)中调用。 xaml),然后我可以在构造函数中设置一个布尔标志,是执行新的数据库查询还是恢复页面的状态(来自 PhoneApplication.Current.State)。布尔标志最初设置为 false,仅在 SecondPage.xaml.cs 的构造函数中设置为 true。
我认为这很好用,但是当我按下开始按钮离开应用程序,然后点击后退按钮返回应用程序时,SecondPage.xaml.cs 的构造函数被调用。所以我做了另一个新的数据库查询,而不是恢复状态,这不是预期的行为。
我的问题是,当用户点击开始然后返回应用程序时,我如何知道何时进行新的数据库查询或恢复?我自己也想过如何解决这个问题,但我想到的大部分都是拼凑的;这看起来很不自然,就好像我在修补让事情正常运转一样。例如,我认为我可以将查询字符串从 FirstPage.xaml 传递到 SecondPage.xaml (即 /SecondPage.xaml?freshDbQuery=1),但是当我从 ThirdPage.xaml 移回 SecondPage.xaml 时,该查询字符串键值对,freshDbQuery=1,总是如此! (所以你可以看出,我不太了解wp7)。
任何帮助表示赞赏。
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
in other words,
main page -> page with list of objects -> page displaying one object in detail from previous page
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.
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()).
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.
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.
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).
any help is appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
所有逻辑删除处理都应在
OnNavigatingFrom
** 和OnNavigedTo
事件中完成。您可以通过以下方式为您的情况创建通用处理程序:
** 尽可能优先使用此处理程序而不是
OnNavigateFrom
。All your handling for tombstoning should be done in the
OnNavigatingFrom
** and theOnNavigatedTo
events.You can create all purpose handlers for your situation with the following:
** Use this in preference to
OnNavigatedFrom
wherever possible.