win phone 8导航问题的处理
导航不善用,非常麻烦,容易走进死循环,抄近路的解决办法是,
1.重写返回键的时候强制导航到哪个页面:
protected override void OnBackKeyPress(CancelEventArgs e) { NavigationService.Navigate(new Uri("/xxx.xaml", UriKind.Relative)); base.OnBackKeyPress(e); }
2.清空导航栈中的参数:参考自:http://xiaoxiao123.blog.51cto.com/5568168/939592
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e) { base.OnNavigatedTo(e); if (NavigationContext.QueryString.ContainsKey("id")) { string id= NavigationContext.QueryString["id"]; if (id=="1") { NavigationContext.QueryString.Remove("id");//这句是关键 NavigationService.Navigate(new Uri("page3.xaml",UriKind.Relative)); } }
3.很重要的一个东东:
判断从哪进来的问题: if (e.NavigationMode == NavigationMode.New)
4.当页面离开的时候,传过此页面的参数将会消失,eg:
protected override void OnBackKeyPress(CancelEventArgs e) { if (NavigationContext.QueryString.ContainsKey("busflag")) { NavigationService.Navigate(new Uri("/Mainpage.xaml", UriKind.Relative)); //离开时候已被销毁,不用remove //NavigationContext.QueryString.Remove("busflag"); } base.OnBackKeyPress(e); }
参考自:http://blog.csdn.net/cc_net/article/details/6581281