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

我在我的WPF项目中使用MahApps.Metro ,我正在构建一个类来帮助我显示Dialogs 。 我想知道是否有办法在显示另一个之前关闭所有可见对话框。

有时,当我显示ProgressDialog然后显示MessageDialog ,ProgressDialog没有正确关闭,并且保持在后台,所以当我关闭MessageDialog时,它会在那里冻结UI。

GIF要说明

这是我目前正在尝试隐藏所有对话框的方式:

 public static async void HideVisibleDialogs(MetroWindow parent) { BaseMetroDialog dialogBeingShow = await parent.GetCurrentDialogAsync(); while (dialogBeingShow != null) { await parent.HideMetroDialogAsync(dialogBeingShow); dialogBeingShow = await parent.GetCurrentDialogAsync(); } } 

我称之为:

 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] } 

任何帮助表示赞赏。 谢谢!

@编辑

显然,由于Thomas Freudenberg ,这个问题似乎已经解决了

这是现在的样子:

 public static Task HideVisibleDialogs(MetroWindow parent) { return Task.Run(async () => { await parent.Dispatcher.Invoke(async () => { BaseMetroDialog dialogBeingShow = await parent.GetCurrentDialogAsync(); while (dialogBeingShow != null) { await parent.HideMetroDialogAsync(dialogBeingShow); dialogBeingShow = await parent.GetCurrentDialogAsync(); } }); }); } 

我称之为:

 HideVisibleDialogs(parent).Wait(); 

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