How to close Message Dialog programmatically

a 夏天 提交于 2019-12-19 08:01:22

问题


I am trying to close a MessageDialog in my WinRT App. I have noticed if I attempt to show two message dialogs at once, I get an UnauthorizedAccessException. To avoid this, I want to close the existing message dialog if it is open. I use this to show the dialog:

    MessageDialog md = new MessageDialog(" ");

    private void MessageBox(string s)
    {
        Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
        {
            md.Content = s;
            //CLOSE HERE
            md.ShowAsync();
        }
        );
    }

How do I close it?


回答1:


instead of trying to find a way to close it, try this declare a instance variable for AsyncCommand;

AsyncCommand command;

command = md.ShowAsync();

then in your commandhandler, before running your method check if command is null

if(command!=null)
{
command.Cancel();
}

// do stuff/ tryagain block



来源:https://stackoverflow.com/questions/12698666/how-to-close-message-dialog-programmatically

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