我遇到了一个问题,在模拟器中的某些实例上,当我单击后面的硬件按钮时,后面的页面会在调用构造函数时加载,而其他时候不会调用构造函数。为什么这样?这是因为它是模拟器吗?
答案 0 :(得分:0)
你是如何进行导航的?您是否取消了初始OnNavigatingFrom以执行动画,然后在动画完成后再次监听启动导航?
protected override void OnNavigatingFrom(System.Windows.Navigation.NavigatingCancelEventArgs e)
{
if (_pendingNavigation == null)
{
VisualStateManager.GoToState(this, "LeavingPage", true);
_pendingNavigation = e.Uri;
e.Cancel = true;
}
base.OnNavigatingFrom(e);
}
void LeavingPage_Completed(object sender, EventArgs e)
{
var uri = _pendingNavigation;
NavigationService.Navigate(uri);
_pendingNavigation = null;
}
当您调用NavigationService.Navigate()时会发生错误,然后将新页面实例添加到导航堆栈。要修复此错误,您需要检查并确保初始页面导航是“新建”导航。像这样:
if (e.NavigationMode == NavigationMode.New && _pendingNavigation == null)
{
VisualStateManager.GoToState(this, "LeavingPage", true);
_pendingNavigation = e.Uri;
e.Cancel = true;
}