我有条件在加载页面之前检查。如果条件成立,我想在加载当前页面之前导航到此页面。我做了一点研究,得知我不能在构造函数中使用NavigationService。我怎样才能达到我想做的目的?
if (!App.appSettings.Contains("citySelected"))
{
NavigationService.Navigate(new Uri("/Pages/CityList.xaml", UriKind.Relative));
}
答案 0 :(得分: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));
}