其他分享
首页 > 其他分享> > 在显示另一对话框之前,隐藏所有可见的都市对话框

在显示另一对话框之前,隐藏所有可见的都市对话框

作者:互联网

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

有时,当我显示一个ProgressDialog然后一个MessageDialog时,ProgressDialog没有正确关闭,并停留在后台,因此当我关闭MessageDialog时,它停留在冻结UI的位置.

GIF to ilustrate

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

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

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

@编辑

显然,这个问题似乎已经解决,这要归功于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是一个异步方法.我将尝试将其返回类型更改为Task并等待它,即HideVisibleDialogs(parent).Wait().否则,呼叫将立即返回.

标签:mahapps-metro,dialog,wpf,c
来源: https://codeday.me/bug/20191118/2028917.html