Hide all visible Metro Dialogs before showing another one

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-05 02:33:00

问题


I'm using MahApps.Metro on my WPF project, and I am building a class to help me showing Dialogs. I would like to know if there's a way of closing all visible dialogs before showing up another one.

Sometimes, when I show a ProgressDialog and then a MessageDialog the ProgressDialog isn't correctly closed, and stays in the background, so when I close the MessageDialog, it stays there freezing the UI.

Here's how I'm currently trying to hide all Dialogs:

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>();
    }
}

I call it like this:

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

Any help is appreciated. Thank you!

@EDIT

Apparently, the problem seems to be solved, thanks to Thomas Freudenberg

This is how it is now:

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>();
            }
        });
    });      
}

And I call it like this:

HideVisibleDialogs(parent).Wait();

回答1:


HideVisibleDialogs is an async method. I'd try to change its return type to Task and wait for it, i.e. HideVisibleDialogs(parent).Wait(). Otherwise the call would immediately return.



来源:https://stackoverflow.com/questions/37789431/hide-all-visible-metro-dialogs-before-showing-another-one

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!