在 WFP MahApps 应用程序中,将 "e.Cancel" 恢复为 false 不起作用
In a WFP MahApps application, restoring "e.Cancel" to false doesn't work
在以下异步 OnNavigating 处理程序中,将 e.Cancel 恢复为 false 应该允许用户离开当前页面。不知何故导航意外失败!
private async void NavigationService_OnNavigating(object sender, NavigatingCancelEventArgs e)
{
var model = DataContext as ViewModel;
if (model == null || !model.IsDirty) return;
e.Cancel = true;
var option = MessageDialogResult.Negative;
try
{
var metroWindow = (MainWindow)Application.Current.MainWindow;
option = await metroWindow.ShowMessageAsync("Form", "Quit?", MessageDialogStyle.AffirmativeAndNegative);
}
finally
{
if (option == MessageDialogResult.Affirmative)
//******Allow the user to move away********
e.Cancel = false;
}
}
任何人都可以阐明这个问题吗?与 .Net 线程相关的任何内容?
更新:
使用决策变量 _canNavigate
不优雅地解决了该问题。
private bool _canNavigate = false;
private async void NavigationService_OnNavigating(object sender, NavigatingCancelEventArgs e)
{
var model = DataContext as ViewModel;
if (model == null || !model.IsDirty) return;
if (!_canNavigate)
{
e.Cancel = true;
var metroWindow = (MainWindow)Application.Current.MainWindow;
var option = await metroWindow.ShowMessageAsync("Form", "Quit?", MessageDialogStyle.AffirmativeAndNegative);
if (option == MessageDialogResult.Affirmative)
{
_canNavigate = true;
_navigationService.Navigate(e.Uri);
}
else _canNavigate = false;
}
else
{
_canNavigate = false;
}
}
问题已被决策变量 _canNavigate
粗略地解决。
private bool _canNavigate = false;
private async void NavigationService_OnNavigating(object sender, NavigatingCancelEventArgs e)
{
var model = DataContext as ViewModel;
if (model == null || !model.IsDirty) return;
if (!_canNavigate)
{
e.Cancel = true;
var metroWindow = (MainWindow)Application.Current.MainWindow;
var option = await metroWindow.ShowMessageAsync("Form", "Quit?", MessageDialogStyle.AffirmativeAndNegative);
if (option == MessageDialogResult.Affirmative)
{
_canNavigate = true;
_navigationService.Navigate(e.Uri);
}
else _canNavigate = false;
}
else
{
_canNavigate = false;
}
}
在以下异步 OnNavigating 处理程序中,将 e.Cancel 恢复为 false 应该允许用户离开当前页面。不知何故导航意外失败!
private async void NavigationService_OnNavigating(object sender, NavigatingCancelEventArgs e)
{
var model = DataContext as ViewModel;
if (model == null || !model.IsDirty) return;
e.Cancel = true;
var option = MessageDialogResult.Negative;
try
{
var metroWindow = (MainWindow)Application.Current.MainWindow;
option = await metroWindow.ShowMessageAsync("Form", "Quit?", MessageDialogStyle.AffirmativeAndNegative);
}
finally
{
if (option == MessageDialogResult.Affirmative)
//******Allow the user to move away********
e.Cancel = false;
}
}
任何人都可以阐明这个问题吗?与 .Net 线程相关的任何内容?
更新:
使用决策变量 _canNavigate
不优雅地解决了该问题。
private bool _canNavigate = false;
private async void NavigationService_OnNavigating(object sender, NavigatingCancelEventArgs e)
{
var model = DataContext as ViewModel;
if (model == null || !model.IsDirty) return;
if (!_canNavigate)
{
e.Cancel = true;
var metroWindow = (MainWindow)Application.Current.MainWindow;
var option = await metroWindow.ShowMessageAsync("Form", "Quit?", MessageDialogStyle.AffirmativeAndNegative);
if (option == MessageDialogResult.Affirmative)
{
_canNavigate = true;
_navigationService.Navigate(e.Uri);
}
else _canNavigate = false;
}
else
{
_canNavigate = false;
}
}
问题已被决策变量 _canNavigate
粗略地解决。
private bool _canNavigate = false;
private async void NavigationService_OnNavigating(object sender, NavigatingCancelEventArgs e)
{
var model = DataContext as ViewModel;
if (model == null || !model.IsDirty) return;
if (!_canNavigate)
{
e.Cancel = true;
var metroWindow = (MainWindow)Application.Current.MainWindow;
var option = await metroWindow.ShowMessageAsync("Form", "Quit?", MessageDialogStyle.AffirmativeAndNegative);
if (option == MessageDialogResult.Affirmative)
{
_canNavigate = true;
_navigationService.Navigate(e.Uri);
}
else _canNavigate = false;
}
else
{
_canNavigate = false;
}
}