1
- using System ;
1
+ using System ;
2
2
using System . Collections . Generic ;
3
3
using System . Linq ;
4
4
using System . Threading . Tasks ;
@@ -285,8 +285,17 @@ private static void IsOpenPropertyChangedCallback(DependencyObject dependencyObj
285
285
object ? closeParameter = null ;
286
286
if ( dialogHost . CurrentSession is { } session )
287
287
{
288
+ if ( ! session . IsEnded )
289
+ {
290
+ session . Close ( session . CloseParameter ) ;
291
+ }
292
+ //DialogSession.Close may attempt to cancel the closing of the dialog.
293
+ //When the dialog is closed in this manner it is not valid
294
+ if ( ! session . IsEnded )
295
+ {
296
+ throw new InvalidOperationException ( $ "Cannot cancel dialog closing after { nameof ( IsOpen ) } property has been set to { bool . FalseString } ") ;
297
+ }
288
298
closeParameter = session . CloseParameter ;
289
- session . IsEnded = true ;
290
299
dialogHost . CurrentSession = null ;
291
300
}
292
301
@@ -524,14 +533,10 @@ public event DialogOpenedEventHandler DialogOpened
524
533
"DialogOpenedAttached" , typeof ( DialogOpenedEventHandler ) , typeof ( DialogHost ) , new PropertyMetadata ( default ( DialogOpenedEventHandler ) ) ) ;
525
534
526
535
public static void SetDialogOpenedAttached ( DependencyObject element , DialogOpenedEventHandler value )
527
- {
528
- element . SetValue ( DialogOpenedAttachedProperty , value ) ;
529
- }
536
+ => element . SetValue ( DialogOpenedAttachedProperty , value ) ;
530
537
531
538
public static DialogOpenedEventHandler GetDialogOpenedAttached ( DependencyObject element )
532
- {
533
- return ( DialogOpenedEventHandler ) element . GetValue ( DialogOpenedAttachedProperty ) ;
534
- }
539
+ => ( DialogOpenedEventHandler ) element . GetValue ( DialogOpenedAttachedProperty ) ;
535
540
536
541
public static readonly DependencyProperty DialogOpenedCallbackProperty = DependencyProperty . Register (
537
542
nameof ( DialogOpenedCallback ) , typeof ( DialogOpenedEventHandler ) , typeof ( DialogHost ) , new PropertyMetadata ( default ( DialogOpenedEventHandler ) ) ) ;
@@ -546,9 +551,7 @@ public DialogOpenedEventHandler? DialogOpenedCallback
546
551
}
547
552
548
553
protected void OnDialogOpened ( DialogOpenedEventArgs eventArgs )
549
- {
550
- RaiseEvent ( eventArgs ) ;
551
- }
554
+ => RaiseEvent ( eventArgs ) ;
552
555
553
556
#endregion
554
557
@@ -577,14 +580,10 @@ public event DialogClosingEventHandler DialogClosing
577
580
"DialogClosingAttached" , typeof ( DialogClosingEventHandler ) , typeof ( DialogHost ) , new PropertyMetadata ( default ( DialogClosingEventHandler ) ) ) ;
578
581
579
582
public static void SetDialogClosingAttached ( DependencyObject element , DialogClosingEventHandler value )
580
- {
581
- element . SetValue ( DialogClosingAttachedProperty , value ) ;
582
- }
583
+ => element . SetValue ( DialogClosingAttachedProperty , value ) ;
583
584
584
585
public static DialogClosingEventHandler GetDialogClosingAttached ( DependencyObject element )
585
- {
586
- return ( DialogClosingEventHandler ) element . GetValue ( DialogClosingAttachedProperty ) ;
587
- }
586
+ => ( DialogClosingEventHandler ) element . GetValue ( DialogClosingAttachedProperty ) ;
588
587
589
588
public static readonly DependencyProperty DialogClosingCallbackProperty = DependencyProperty . Register (
590
589
nameof ( DialogClosingCallback ) , typeof ( DialogClosingEventHandler ) , typeof ( DialogHost ) , new PropertyMetadata ( default ( DialogClosingEventHandler ) ) ) ;
@@ -599,9 +598,7 @@ public DialogClosingEventHandler? DialogClosingCallback
599
598
}
600
599
601
600
protected void OnDialogClosing ( DialogClosingEventArgs eventArgs )
602
- {
603
- RaiseEvent ( eventArgs ) ;
604
- }
601
+ => RaiseEvent ( eventArgs ) ;
605
602
606
603
#endregion
607
604
@@ -616,16 +613,16 @@ internal void AssertTargetableContent()
616
613
internal void InternalClose ( object ? parameter )
617
614
{
618
615
var currentSession = CurrentSession ?? throw new InvalidOperationException ( $ "{ nameof ( DialogHost ) } does not have a current session") ;
619
- var dialogClosingEventArgs = new DialogClosingEventArgs ( currentSession , DialogClosingEvent ) ;
620
616
621
617
currentSession . CloseParameter = parameter ;
622
618
currentSession . IsEnded = true ;
623
619
624
620
//multiple ways of calling back that the dialog is closing:
625
621
// * routed event
626
622
// * the attached property (which should be applied to the button which opened the dialog
627
- // * straight forward dependency property
623
+ // * straight forward IsOpen dependency property
628
624
// * handler provided to the async show method
625
+ var dialogClosingEventArgs = new DialogClosingEventArgs ( currentSession , DialogClosingEvent ) ;
629
626
OnDialogClosing ( dialogClosingEventArgs ) ;
630
627
_attachedDialogClosingEventHandler ? . Invoke ( this , dialogClosingEventArgs ) ;
631
628
DialogClosingCallback ? . Invoke ( this , dialogClosingEventArgs ) ;
@@ -690,21 +687,14 @@ private void OpenDialogHandler(object sender, ExecutedRoutedEventArgs executedRo
690
687
691
688
if ( _popupContentControl != null )
692
689
{
693
- switch ( OpenDialogCommandDataContextSource )
690
+ _popupContentControl . DataContext = OpenDialogCommandDataContextSource switch
694
691
{
695
- case DialogHostOpenDialogCommandDataContextSource . SenderElement :
696
- _popupContentControl . DataContext =
697
- ( executedRoutedEventArgs . OriginalSource as FrameworkElement ) ? . DataContext ;
698
- break ;
699
- case DialogHostOpenDialogCommandDataContextSource . DialogHostInstance :
700
- _popupContentControl . DataContext = DataContext ;
701
- break ;
702
- case DialogHostOpenDialogCommandDataContextSource . None :
703
- _popupContentControl . DataContext = null ;
704
- break ;
705
- default :
706
- throw new ArgumentOutOfRangeException ( ) ;
707
- }
692
+ DialogHostOpenDialogCommandDataContextSource . SenderElement
693
+ => ( executedRoutedEventArgs . OriginalSource as FrameworkElement ) ? . DataContext ,
694
+ DialogHostOpenDialogCommandDataContextSource . DialogHostInstance => DataContext ,
695
+ DialogHostOpenDialogCommandDataContextSource . None => null ,
696
+ _ => throw new ArgumentOutOfRangeException ( ) ,
697
+ } ;
708
698
}
709
699
710
700
DialogContent = executedRoutedEventArgs . Parameter ;
@@ -716,9 +706,7 @@ private void OpenDialogHandler(object sender, ExecutedRoutedEventArgs executedRo
716
706
}
717
707
718
708
private void CloseDialogCanExecute ( object sender , CanExecuteRoutedEventArgs canExecuteRoutedEventArgs )
719
- {
720
- canExecuteRoutedEventArgs . CanExecute = CurrentSession != null ;
721
- }
709
+ => canExecuteRoutedEventArgs . CanExecute = CurrentSession != null ;
722
710
723
711
private void CloseDialogHandler ( object sender , ExecutedRoutedEventArgs executedRoutedEventArgs )
724
712
{
@@ -730,19 +718,13 @@ private void CloseDialogHandler(object sender, ExecutedRoutedEventArgs executedR
730
718
}
731
719
732
720
private string GetStateName ( )
733
- {
734
- return IsOpen ? OpenStateName : ClosedStateName ;
735
- }
721
+ => IsOpen ? OpenStateName : ClosedStateName ;
736
722
737
723
private void OnUnloaded ( object sender , RoutedEventArgs routedEventArgs )
738
- {
739
- LoadedInstances . Remove ( this ) ;
740
- }
724
+ => LoadedInstances . Remove ( this ) ;
741
725
742
726
private void OnLoaded ( object sender , RoutedEventArgs routedEventArgs )
743
- {
744
- LoadedInstances . Add ( this ) ;
745
- }
727
+ => LoadedInstances . Add ( this ) ;
746
728
747
729
}
748
730
}
0 commit comments