为什么这个C#代码不按顺序执行?不是ASYNC(至少我认为不是)

问题描述 投票:-1回答:1

任何人都可以帮我理解为什么在CanNavigateAway函数返回其值后我对dialogservice的调用会执行吗? (我的目标是警告用户他们即将离开视图而不保存他们的更改。如果他们单击OK,则允许导航。我正在使用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;
    }
}        
c# uwp mvvm-light
1个回答
1
投票

如果您需要用户的回复,这些解决方案都不会起作用。问题是,当代码在导航事件处理程序中时,它在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, ... )
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.