任何人都可以帮助我理解为什么在 CanNavigateAway 函数返回其值后执行我对 dialogservice 的调用?(我的目标是警告用户他们将要离开视图而不保存更改。如果他们单击“确定”,则允许导航。我正在使用 MVVM Light。
当我单步执行代码时,它确实到达了对话服务,但在创建对话之前继续到 CanNavigateAway 的末尾。CanNavigateAway 方法由 OnNavigatingFrom 调用。
public bool CanNavigateAway()
{
if (!changesSaved && Model.IsModified && !continueNavigation)
{
dialogService.ShowMessage("Are you sure you want to continue?",
"Confirmation",
buttonConfirmText: "Continue", buttonCancelText: "Discard",
afterHideCallback: (confirmed) =>
{
if (confirmed)
{
// User has pressed the "confirm" button.
// ...
continueNavigation = true;
}
else
{
// User has pressed the "cancel" button
// (or has discared the dialog box).
// ...
continueNavigation = false;
}
});
return continueNavigation;
}
}
这是来自 MVVM Light Bindable Page 类的 OnNavigatingFrom 方法:
protected override void OnNavigatingFrom(NavigatingCancelEventArgs e)
{
var navigableViewModel = this.DataContext as INavigable;
if (navigableViewModel != null)
{
if (!navigableViewModel.CanNavigateAway())
{
e.Cancel = true;
}
}
}
我尝试了一种不同的方式来让对话服务摆脱困境,但 showConfirmationDialogAsync 似乎仍然没有及时执行:
public bool CanNavigateAway()
{
continueNavigation = false;
if (!changesSaved && Model.IsModified && !continueNavigation)
{
showConfirmationDialogAsync();
return continueNavigation;
}
private async void showConfirmationDialogAsync()
{
continueNavigation = false;
ContentDialog noSaveConfirmation = new ContentDialog
{
Title = "Warning",
Content = "You have unsaved changes. Are you sure you want to leave this page without saving?",
PrimaryButtonText = "Leave without saving",
SecondaryButtonText = "Stay and finish"
};
ContentDialogResult result = await noSaveConfirmation.ShowAsync();
if (result == ContentDialogResult.Primary)
{
continueNavigation = true;
}
else if (result == ContentDialogResult.Secondary)
{
continueNavigation = false;
}
}
如果您需要用户的响应,则所有解决方案都不起作用。问题在于,当代码在导航事件处理程序中时,它在 UI 线程上运行并且用户提示异步运行,因此 UI 可以自由地向用户呈现对话框。然而,这意味着事件处理程序在用户有机会响应之前完成。
但是,您可以使用变通解决方案。添加一个标志bool
字段,如forceNavigation
. 然后在里面OnNavigatingFrom
向用户显示对话框并设置Cancel
为true
立即向用户显示确认对话框。如果用户说是,则设置forceNavigaiton
为 true 并再次手动触发导航。现在它将跳过确认部分并立即导航。
protected async override void OnNavigatingFrom(NavigatingCancelEventArgs e)
{
//if navigation is forced, skip all logic
if ( !forceNavigation )
{
var navigableViewModel = this.DataContext as INavigable;
if (navigableViewModel != null)
{
e.Cancel = true;
//display the dialog to the user, if he says yes, set
//forceNavigation = true; and repeat the navigation (e.g. GoBack, ... )
}
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句