隐藏所有可见的Metro对话框,然后再显示另一个 [英] Hide all visible Metro Dialogs before showing another one

查看:200
本文介绍了隐藏所有可见的Metro对话框,然后再显示另一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在WPF项目中使用



这是我目前的情况尝试隐藏所有对话框:

 公共静态异步void HideVisibleDialogs(MetroWindow父级)
{
BaseMetroDialog dialogBeingShow =等待父对象。GetCurrentDialogAsync< BaseMetroDialog>();

while(dialogBeingShow!= null)
{
等待父对象。HideMetroDialogAsync(dialogBeingShow);
dialogBeingShow =等待父对象。GetCurrentDialogAsync< BaseMetroDialog>();
}
}

我这样称呼它:

 公共静态MessageDialogResult ShowMessage(字符串标题,字符串消息,MetroWindow父级,Int32超时,MessageDialogStyle样式,MetroDialogSettings设置,MessageDialogResult defaultResult)
{
AutoResetEvent arEvent =新的AutoResetEvent(false);

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

arEvent.WaitOne();

[方法的其余部分]
}

任何帮助都是赞赏。谢谢!



@EDIT



显然,问题似乎已经解决了,这要感谢 Thomas Freudenberg



现在是:

 公共静态任务HideVisibleDialogs(MetroWindow父级)
{
return Task.Run(async ()=>
{
等待父对象。Dispatcher.Invoke(async()=>
{
BaseMetroDialog dialogBeingShow =等待父对象。GetCurrentDialogAsync< BaseMetroDialog>();

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

我这样称呼它:

  HideVisibleDialogs(parent).Wait(); 


解决方案

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


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

解决方案

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.

这篇关于隐藏所有可见的Metro对话框,然后再显示另一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆