Only single ContentDialog will be opened. While opening second dialog, how would i know that ContentDialog is already opened and how to call its hide()?
Tried with VisualTreeHelper.GetOpenPopups(Window.Current) but no use.
Only single ContentDialog will be opened. While opening second dialog, how would i know that ContentDialog is already opened and how to call its hide()?
Tried with VisualTreeHelper.GetOpenPopups(Window.Current) but no use.
I ended up writing a static method to help me with that:
using System.Threading; using System.Threading.Tasks; using Windows.UI.Xaml.Controls; /// <summary> /// In order to manage only one open contentdialog at a time, use this semaphore /// </summary> public static SemaphoreSlim contentDialogSemaphore { get; } = new SemaphoreSlim(1); /// <summary> /// Use this helper method to show your ContentDialog within the semaphore to avoid collisions /// </summary> /// <param name="dlg"></param> /// <returns>Null if there was no result, or the ContentDialogResult</returns> public static async Task<ContentDialogResult?> showContentDialogInSemaphore(ContentDialog dlg) { ContentDialogResult? result = null; if (dlg != null) try { await contentDialogSemaphore.WaitAsync(); result = await dlg.ShowAsync(); } finally { contentDialogSemaphore.Release(); } return result; }
Now all I need to do is call this method instead of the ShowAsync()
of the ContentDialog
. It makes sure that only one ContentDialog
is open at all times, and stacks the requests for dialogs, waiting nicely for the user to interact.
Derive from official document, there can only be one ContentDialog open per thread at a time. Attempting to open two ContentDialogs will throw an exception, even if they are attempting to open in separate AppWindows.
So you don’t need to hide previously opened ContentDialog in uwp. When you could show a ContentDialog, it indicates that only this ContentDialog is opened in the current thread.