【问题标题】:How to navigate to a page with a condition before loading current page?如何在加载当前页面之前导航到具有条件的页面?
【发布时间】:2014-08-13 12:36:35
【问题描述】:
我有一个条件要在加载页面之前检查。如果条件成立,我想在加载当前页面之前导航到此页面。我做了一些研究,了解到我不能在构造函数中使用 NavigationService。我怎样才能实现我想做的事情?
if (!App.appSettings.Contains("citySelected"))
{
NavigationService.Navigate(new Uri("/Pages/CityList.xaml", UriKind.Relative));
}
【问题讨论】:
标签:
c#
windows-phone-8
navigation
【解决方案1】:
虽然 NavigationService 不能在页面的构造函数中使用,但可以在其 Loaded 事件处理程序中使用:
public FirstPage()
{
this.InitializeComponent();
this.Loaded += (sender, args) =>
{
if (whatever == true)
NavigationService.Navigate(new Uri("/Pages/SecondPage.xaml", UriKind.Relative));
};
}
或者,页面的OnNavigatedTo 方法也可以:
protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
if (whatever == true)
NavigationService.Navigate(new Uri("/SecondPage.xaml", UriKind.Relative));
}