Search code examples
c#uwp

How to check if ANY ContentDialog is open?


So we can only have one opened content dialog at a time. This is fine. But in my app there are several possible content dialogs that can be opened, and I would like to avoid making my own variable because I can forget to add it somewhere and the whole app will crash (because trying to open second content dialog throws exception).

So my question is: How to check if any ContentDialog is open?

Note:

  1. I don't want to check for specific ContentDialog.
  2. I would like to avoid creating my own variables.

Solution

  • ContentDialog is shown in the PopupRoot so using VisualTreeHelper.GetOpenPopups() will help you get it.

    var openedpopups = VisualTreeHelper.GetOpenPopups(Window.Current);
    foreach (var popup in openedpopups)
    {
       if(popup.Child is ContentDialog)
       {
          //some content dialog is open.
       }
    }