我已按照教程here来保留页面之间的状态。
然而,当我以编程方式导航回此页面时, State 字典总是空的!这里似乎有什么问题?
我在Emulator 8.0.10322 720P上测试了这种行为。
这就是我的所作所为:
protected override void OnNavigatedFrom(NavigationEventArgs e)
{
base.OnNavigatedFrom(e);
if (e.NavigationMode != NavigationMode.Back)
{
State["IS"] = ResultsList.ItemsSource;
State["SI"] = ResultsList.SelectedItem;
State["dgtest"] = new Data();
State["test"] = "gi2";
}
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
if (e.IsNavigationInitiator && e.NavigationMode != NavigationMode.Back)
{
ResultsList.ItemsSource = (System.Collections.IList) State["IS"];
ResultsList.SelectedItem = State["SI"];
}
}
ItemsSource 中的对象属于Data类型,在此处定义:
[DataContract]
public class Data
{
[DataMember]
public string Datum { get; set; }
}
编辑:以编程方式导航回来,我正在呼叫NavigationService.Navigate(new Uri("/mypages/page.xaml", UriKind.Relative));