UWP 确认是否有弹窗显示

当应用已存在弹窗时,再显示其他弹窗,会提示

某个异步操作没有正常启动。

Only a single ContentDialog can be open at any time.
   at Windows.UI.Xaml.Controls.ContentDialog.ShowAsync()
   at .......
   at System.Threading.WinRTSynchronizationContextBase.Invoker.InvokeCore()

解决思路:判断当前是否有弹窗被展示

protected bool IsAnyContentDialogOpen()
{
    return VisualTreeHelper.GetOpenPopups(Window.Current).Count > 0;
}

或者

var openedpopups = VisualTreeHelper.GetOpenPopups(Window.Current);
foreach (var popup in openedpopups)
{
   if(popup.Child is ContentDialog)
   {
      //有ContentDialog弹窗被展示
   }
}

参考资料

How to check if ANY ContentDialog is open?

posted @ 2022-02-17 19:37  Lulus  阅读(219)  评论(0编辑  收藏  举报