在显示另一个之前隐藏所有可见的 Metro 对话框

Hide all visible Metro Dialogs before showing another one

我正在使用 MahApps.Metro on my WPF project, and I am building a class to help me showing Dialogs我想知道是否有一种方法可以在显示另一个对话框之前关闭所有可见对话框。

有时,当我显示 ProgressDialog 然后显示 MessageDialog 时,ProgressDialog 没有正确关闭,而是停留在后台,所以当我关闭 MessageDialog 时,它会停留在那里冻结UI.

这是我目前试图隐藏所有对话框的方法:

public static async void HideVisibleDialogs(MetroWindow parent)
{
    BaseMetroDialog dialogBeingShow = await parent.GetCurrentDialogAsync<BaseMetroDialog>();

    while (dialogBeingShow != null)
    {
        await parent.HideMetroDialogAsync(dialogBeingShow);
        dialogBeingShow = await parent.GetCurrentDialogAsync<BaseMetroDialog>();
    }
}

我这样称呼它:

public static MessageDialogResult ShowMessage(String title, String message, MetroWindow parent, Int32 timeout, MessageDialogStyle style, MetroDialogSettings settings, MessageDialogResult defaultResult)
{
    AutoResetEvent arEvent = new AutoResetEvent(false);

    App.Current.Dispatcher.Invoke(() =>
    {
        HideVisibleDialogs(parent);
        arEvent.Set();
    });

    arEvent.WaitOne();

    [Rest of method]
}

感谢任何帮助。谢谢!

@EDIT

看来,问题似乎解决了,感谢Thomas Freudenberg

现在是这样:

public static Task HideVisibleDialogs(MetroWindow parent)
{
    return Task.Run(async () => 
    {
        await parent.Dispatcher.Invoke(async () =>
        {
            BaseMetroDialog dialogBeingShow = await parent.GetCurrentDialogAsync<BaseMetroDialog>();

            while (dialogBeingShow != null)
            {
                await parent.HideMetroDialogAsync(dialogBeingShow);
                dialogBeingShow = await parent.GetCurrentDialogAsync<BaseMetroDialog>();
            }
        });
    });      
}

我这样称呼它:

HideVisibleDialogs(parent).Wait();

HideVisibleDialogs 是一种异步方法。我会尝试将其 return 类型更改为 Task 并等待它,即 HideVisibleDialogs(parent).Wait()。否则调用会立即 return.