我正在 Silverlight 中构建一个 wp7 应用程序。我所有应用程序的状态都存储在NavigationContext.QueryString
. 如果这可以在应用程序停用时保存,并且该页面在应用程序重新激活时导航到,那将满足我对墓碑的要求。
但是,我不太确定如何执行此操作。我正在考虑保存NavigationContext.QueryString
到State
字典中App.xaml.cs::Application_Deactivated()
,但该代码无权访问NavigationContext.QueryString
. 还有其他方法可以做到这一点吗?
我想我可以在每次导航时将查询字符串保存到 State 字典中,然后在重新激活应用程序时恢复它。还是有更好的方法?
更新:根据indyfromoz的回答,我想实现以下
OnNavigatedToHandler()
{
// save NavigationContext.QueryString in the State dictionary
}
为了减少冗余,我想我会在继承自的类中实现这一点PhoneApplicationPage
,然后让我的所有其余页面都继承自该类。但是,我得到的问题是所有页面类都是partial
因为它们也在生成的代码中定义。我不想更改生成的代码,因为每次重新生成时都重新更改它会很痛苦。
有一个更好的方法吗?
更新 2:这是我现在在我的应用程序的主页(在启动时导航到的那个)一起破解的内容:
public partial class MainPivot : PhoneApplicationPage
{
public MainPivot()
{
InitializeComponent();
Loaded += new RoutedEventHandler(MainPivot_Loaded);
PhoneApplicationService.Current.Deactivated += new EventHandler<DeactivatedEventArgs>(App_Deactivated);
MessageBox.Show("launching main pivot (state count: " + PhoneApplicationService.Current.State.Count + ")");
if (PhoneApplicationService.Current.State.Count != 0)
{
Debug.Assert(PhoneApplicationService.Current.State.ContainsKey(QueryStringKey),
"State is initialized, but contains no value for the query string");
string oldQueryString = (string)PhoneApplicationService.Current.State[QueryStringKey];
MessageBox.Show("Old query string: " + oldQueryString);
NavigationService.Navigate(new Uri(oldQueryString));
}
}
public readonly string QueryStringKey = "queryString";
void App_Deactivated(object sender, DeactivatedEventArgs e)
{
PhoneApplicationService.Current.State[QueryStringKey] = NavigationService.Source;
}
// ...
它有效(排序),但它很丑陋。
更新 3:看起来 wp7 操作系统会在基于页面的应用程序中自动重新加载正确的页面。我实际上正在使用页面,所以也许我不需要在这里做太多的工作。
但是,它似乎不起作用。我启动应用程序,转到一个页面,点击“开始”,然后点击“返回”。屏幕显示“正在恢复...”,但似乎挂在那里。此时我的代码是否应该以某种方式响应?有没有办法即使在点击“开始”后也可以保持调试器的连接?