@@ -74,6 +74,8 @@ public ContentPopup()
7474 }
7575 }
7676
77+ Closing += OnClosing ;
78+
7779 Connected += OnLoaded ;
7880 Disconnected += OnUnloaded ;
7981
@@ -84,6 +86,14 @@ public ContentPopup()
8486 this . RegisterPropertyChangedCallback ( CloseButtonTextProperty , OnButtonTextChanged , ref _closeTextToken ) ;
8587 }
8688
89+ private ContentDialogResult _closingResult ;
90+ private TaskCompletionSource < ContentDialogResult > _closingTask ;
91+
92+ private void OnClosing ( ContentDialog sender , ContentDialogClosingEventArgs args )
93+ {
94+ _closingResult = args . Result ;
95+ }
96+
8797 private void OnCloseButtonClick ( ContentDialog sender , ContentDialogButtonClickEventArgs args )
8898 {
8999 // For some weird reason, there's no ContentDialogResult.Close, so we hack it around.
@@ -190,18 +200,18 @@ private void OnLoaded(object sender, RoutedEventArgs e)
190200 this . RegisterPropertyChangedCallback ( SecondaryButtonTextProperty , OnButtonTextChanged , ref _secondaryTextToken ) ;
191201 this . RegisterPropertyChangedCallback ( CloseButtonTextProperty , OnButtonTextChanged , ref _closeTextToken ) ;
192202
193- try
194- {
195- if ( XamlRoot . Content is IPopupHost host )
196- {
197- host . PopupOpened ( ) ;
198- }
199- }
200- catch
201- {
202- // XamlRoot.Content seems to throw a NullReferenceException
203- // whenever corresponding window has been already closed.
204- }
203+ // try
204+ // {
205+ // if (XamlRoot.Content is IPopupHost host)
206+ // {
207+ // host.PopupOpened();
208+ // }
209+ // }
210+ // catch
211+ // {
212+ // // XamlRoot.Content seems to throw a NullReferenceException
213+ // // whenever corresponding window has been already closed.
214+ // }
205215
206216 var canvas = VisualTreeHelper . GetParent ( this ) as Canvas ;
207217 if ( canvas != null )
@@ -249,18 +259,18 @@ private void OnUnloaded(object sender, RoutedEventArgs e)
249259 this . UnregisterPropertyChangedCallback ( SecondaryButtonTextProperty , ref _secondaryTextToken ) ;
250260 this . UnregisterPropertyChangedCallback ( CloseButtonTextProperty , ref _closeTextToken ) ;
251261
252- try
253- {
254- if ( XamlRoot . Content is IPopupHost host )
255- {
256- host . PopupClosed ( ) ;
257- }
258- }
259- catch
260- {
261- // XamlRoot.Content seems to throw a NullReferenceException
262- // whenever corresponding window has been already closed.
263- }
262+ // try
263+ // {
264+ // if (XamlRoot.Content is IPopupHost host)
265+ // {
266+ // host.PopupClosed();
267+ // }
268+ // }
269+ // catch
270+ // {
271+ // // XamlRoot.Content seems to throw a NullReferenceException
272+ // // whenever corresponding window has been already closed.
273+ // }
264274 }
265275
266276 private void OnProcessKeyboardAccelerators ( UIElement sender , ProcessKeyboardAcceleratorEventArgs args )
@@ -329,6 +339,7 @@ protected override void OnApplyTemplate()
329339
330340 if ( LayoutRoot != null )
331341 {
342+ LayoutRoot . RegisterPropertyChangedCallback ( IsHitTestVisibleProperty , OnIsHitTestVisibleChanged ) ;
332343 LayoutRoot . ProcessKeyboardAccelerators += OnProcessKeyboardAccelerators ;
333344 ElementCompositionPreview . SetIsTranslationEnabled ( LayoutRoot , true ) ;
334345 }
@@ -340,6 +351,79 @@ protected override void OnApplyTemplate()
340351 CalculateButtonsVisualState ( ) ;
341352 }
342353
354+ private void OnIsHitTestVisibleChanged ( DependencyObject sender , DependencyProperty dp )
355+ {
356+ if ( ! LayoutRoot . IsHitTestVisible )
357+ {
358+ VisualUtilities . QueueCallbackForCompositionRendered ( Test ) ;
359+ }
360+ }
361+
362+ private void Test ( )
363+ {
364+ _closingTask ? . TrySetResult ( _closingResult ) ;
365+ }
366+
367+ [ ThreadStatic ]
368+ private static TaskCompletionSource < ContentDialogResult > _currentDialogShowRequest ;
369+
370+ /// <summary>
371+ /// Begins an asynchronous operation showing a dialog.
372+ /// If another dialog is already shown using
373+ /// ShowQueuedAsync or ShowIfPossibleAsync method - it will wait
374+ /// for that previous dialog to be dismissed before showing the new one.
375+ /// </summary>
376+ /// <param name="dialog">The dialog.</param>
377+ /// <returns></returns>
378+ /// <exception cref="InvalidOperationException">This method can only be invoked from UI thread.</exception>
379+ public async Task < ContentDialogResult > ShowQueuedAsync ( XamlRoot xamlRoot )
380+ {
381+ while ( _currentDialogShowRequest != null )
382+ {
383+ await _currentDialogShowRequest . Task ;
384+ }
385+
386+ Logger . Info ( GetType ( ) . Name ) ;
387+
388+ XamlRoot = xamlRoot ;
389+ OnCreate ( ) ;
390+
391+ _closingTask = new TaskCompletionSource < ContentDialogResult > ( ) ;
392+ _currentDialogShowRequest = _closingTask ;
393+ _ = ShowAsync ( ) ;
394+
395+ try
396+ {
397+ if ( XamlRoot . Content is IPopupHost host )
398+ {
399+ host . PopupOpened ( ) ;
400+ }
401+ }
402+ catch
403+ {
404+ // XamlRoot.Content seems to throw a NullReferenceException
405+ // whenever corresponding window has been already closed.
406+ }
407+
408+ var result = await _closingTask . Task ;
409+ _currentDialogShowRequest = null ;
410+
411+ try
412+ {
413+ if ( XamlRoot . Content is IPopupHost host )
414+ {
415+ host . PopupClosed ( ) ;
416+ }
417+ }
418+ catch
419+ {
420+ // XamlRoot.Content seems to throw a NullReferenceException
421+ // whenever corresponding window has been already closed.
422+ }
423+
424+ return result ;
425+ }
426+
343427 private void OnButtonTextChanged ( DependencyObject sender , DependencyProperty dp )
344428 {
345429 if ( dp == PrimaryButtonTextProperty )
@@ -584,19 +668,6 @@ public ContentDialogResult CloseButtonResult
584668
585669 #endregion
586670
587- #region SecondaryBackground
588-
589- public Brush SecondaryBackground
590- {
591- get { return ( Brush ) GetValue ( SecondaryBackgroundProperty ) ; }
592- set { SetValue ( SecondaryBackgroundProperty , value ) ; }
593- }
594-
595- public static readonly DependencyProperty SecondaryBackgroundProperty =
596- DependencyProperty . Register ( "SecondaryBackground" , typeof ( Brush ) , typeof ( ContentPopup ) , new PropertyMetadata ( null ) ) ;
597-
598- #endregion
599-
600671 #region ContentMaxWidth
601672
602673 public double ContentMaxWidth
@@ -782,33 +853,33 @@ private static void OnBlockedClosing(ContentDialog sender, ContentDialogClosingE
782853 args . Cancel = sender . Tag != null ;
783854 }
784855
785- [ ThreadStatic ]
786- private static TaskCompletionSource < ContentDialog > _currentDialogShowRequest ;
856+ // [ThreadStatic]
857+ // private static TaskCompletionSource<ContentDialog> _currentDialogShowRequest;
787858
788- public async Task < ContentDialogResult > ShowQueuedAsync ( XamlRoot xamlRoot )
789- {
790- while ( _currentDialogShowRequest != null )
791- {
792- await _currentDialogShowRequest . Task ;
793- }
859+ // public async Task<ContentDialogResult> ShowQueuedAsync(XamlRoot xamlRoot)
860+ // {
861+ // while (_currentDialogShowRequest != null)
862+ // {
863+ // await _currentDialogShowRequest.Task;
864+ // }
794865
795- var dialog = this ;
796- Logger . Info ( dialog . GetType ( ) . Name ) ;
866+ // var dialog = this;
867+ // Logger.Info(dialog.GetType().Name);
797868
798- if ( dialog is ContentPopup popup )
799- {
800- popup . OnCreate ( ) ;
801- }
869+ // if (dialog is ContentPopup popup)
870+ // {
871+ // popup.OnCreate();
872+ // }
802873
803- dialog . XamlRoot = xamlRoot ;
874+ // dialog.XamlRoot = xamlRoot;
804875
805- var request = _currentDialogShowRequest = new TaskCompletionSource < ContentDialog > ( ) ;
806- var result = await dialog . ShowAsync ( ) ;
807- _currentDialogShowRequest = null ;
808- request . SetResult ( dialog ) ;
876+ // var request = _currentDialogShowRequest = new TaskCompletionSource<ContentDialog>();
877+ // var result = await dialog.ShowAsync();
878+ // _currentDialogShowRequest = null;
879+ // request.SetResult(dialog);
809880
810- Logger . Info ( dialog . GetType ( ) . Name + ", closed" ) ;
811- return result ;
812- }
881+ // Logger.Info(dialog.GetType().Name + ", closed");
882+ // return result;
883+ // }
813884 }
814885}
0 commit comments