首页 > 解决方案 > 关闭主窗口时如何使用 DisplayAlert

问题描述

我在 XAML 中有这个按钮:

<Button
    x:Name="UpdateButton"
    Padding="10"
    Command="{Binding SaveAndUpdateCommand}"
    HorizontalOptions="CenterAndExpand"
    Text="Save and Update"
    VerticalOptions="End" />

在后面的代码中,我设置了如下绑定命令:

SaveAndUpdateCommand = new Command(OnSaveAndUpdate, canExecute: () => { return IsDirty; });

系统运行,当对 GUI 进行更改时,按钮启用。然后在执行保存按钮禁用后。

但我想更进一步,当用户尝试用 X 关闭窗口(左上角)时使用if is DisplayAlertIsDirtytrue

我的第一个想法是覆盖OnDisappearing并制作它async。它正确显示了弹出消息,但此后再也不会关闭窗口。我做了一些研究,很快发现从这个覆盖中显示弹出窗口是个坏主意。

我仍然不确定在 macOS 平台上的 Xamarin.Forms 应用程序中处理此问题的最简单方法是什么。总结一下:

User tries to close the main app window with X at top left
    if IsDirty is true
        Show alert (yes/no) and get response
        if yes then save and continue closing
        if no then just continue closing

理想情况下,它还会提供 Cancel 作为中止关闭的一种方式。

标签: c#macosxamarin.formsvisual-studio-mac

解决方案


推荐阅读