关闭覆盖后无法调用关闭

时间:2018-02-27 18:58:18

标签: c# wpf

我需要一个窗口的实例,所以我写了这段代码:

public static Settings Instance
{
    get
    {
        if (AppWindow == null)
        {
            AppWindow = new Settings();
            //attach the event handler
            AppWindow.Closing += AppWindow_Closing;
        }
        return AppWindow;
    }
}

private static async void AppWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
    e.Cancel = true;

    //call the async method
    bool close = await AppWindow.CheckSettings();
    if (close)
    {
        AppWindow win = (AppWindow)sender;
        //detach the event handler
        AppWindow.Closing -= AppWindow_Closing;
        //...and close the window immediately
        win.Close();
        AppWindow = null;
    }
}

你可以看到我对结束事件进行了覆盖,在其中我称之为CheckSettings()具有这种结构:

private async Task<bool> CheckSettings()
{
     MessageDialogResult result =
                    await this.ShowMessageAsync(Lang.Line("attention", dict), Lang.Line("unsavedChanges", dict),
                    MessageDialogStyle.AffirmativeAndNegative, dialogSettings);

     if(result == MessageDialogResult.Affirmative)
         return true;
     else
         return false;
}

我需要等待MahApp方法ShowMessageAsync()的结果,所以我在结束覆盖中取消了关闭事件。

不幸的是这段代码win.Close()返回一个异常:

  窗口关闭后,

无法设置可见性或调用show或showdialog

我该如何解决这个问题?

0 个答案:

没有答案