我刚刚遇到了 WP7 中臭名昭著的墓碑问题/问题。假设我有 3 页,FirstPage.xaml、SecondPage.xaml 和 ThirdPage.xaml。自然流量将是:
FirstPage.xaml -> SecondPage.xaml -> ThirdPage.xaml
换句话说,
主页 -> 包含对象列表的页面 -> 显示上一页详细对象的页面
当我从 FirstPage.xaml 转到 SecondPage.xaml 时,我必须执行数据库查询以获取 SecondPage.xaml 中的列表。然后我需要从 SecondPage.xaml 转到 ThirdPage.xaml(在我从 List 中选择一个 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.OnPageNavigatedFrom()) 中。
我的(有缺陷的)策略是,如果在一个实例(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)。
任何帮助表示赞赏。