11using System . Collections . Generic ;
2- using System . Linq ;
32using System . Windows ;
43using System . Windows . Controls ;
54using System . Windows . Documents ;
@@ -20,7 +19,7 @@ public class Dialog : ContentControl
2019 private Border _backElement ;
2120 private AdornerContainer _container ;
2221
23- private static readonly Dictionary < string , FrameworkElement > ContainerDict = new ( ) ;
22+ private static readonly ControlTokenManager < FrameworkElement > TokenManager = new ( ) ;
2423 private static readonly Dictionary < string , Dialog > DialogDict = new ( ) ;
2524
2625 public static readonly DependencyProperty IsClosedProperty = DependencyProperty . Register (
@@ -33,7 +32,8 @@ public bool IsClosed
3332 }
3433
3534 public static readonly DependencyProperty MaskCanCloseProperty = DependencyProperty . RegisterAttached (
36- "MaskCanClose" , typeof ( bool ) , typeof ( Dialog ) , new FrameworkPropertyMetadata ( ValueBoxes . FalseBox , FrameworkPropertyMetadataOptions . Inherits ) ) ;
35+ "MaskCanClose" , typeof ( bool ) , typeof ( Dialog ) ,
36+ new FrameworkPropertyMetadata ( ValueBoxes . FalseBox , FrameworkPropertyMetadataOptions . Inherits ) ) ;
3737
3838 public static void SetMaskCanClose ( DependencyObject element , bool value )
3939 => element . SetValue ( MaskCanCloseProperty , ValueBoxes . BooleanBox ( value ) ) ;
@@ -51,22 +51,7 @@ public Brush MaskBrush
5151 }
5252
5353 public static readonly DependencyProperty TokenProperty = DependencyProperty . RegisterAttached (
54- "Token" , typeof ( string ) , typeof ( Dialog ) , new PropertyMetadata ( default ( string ) , OnTokenChanged ) ) ;
55-
56- private static void OnTokenChanged ( DependencyObject d , DependencyPropertyChangedEventArgs e )
57- {
58- if ( d is FrameworkElement element )
59- {
60- if ( e . NewValue == null )
61- {
62- Unregister ( element ) ;
63- }
64- else
65- {
66- Register ( e . NewValue . ToString ( ) , element ) ;
67- }
68- }
69- }
54+ "Token" , typeof ( string ) , typeof ( Dialog ) , new PropertyMetadata ( null , TokenManager . OnTokenChanged ) ) ;
7055
7156 public static void SetToken ( DependencyObject element , string value )
7257 => element . SetValue ( TokenProperty , value ) ;
@@ -79,45 +64,6 @@ public Dialog()
7964 CommandBindings . Add ( new CommandBinding ( ControlCommands . Close , ( s , e ) => Close ( ) ) ) ;
8065 }
8166
82- public static void Register ( string token , FrameworkElement element )
83- {
84- if ( string . IsNullOrEmpty ( token ) || element == null ) return ;
85- ContainerDict [ token ] = element ;
86- }
87-
88- public static void Unregister ( string token , FrameworkElement element )
89- {
90- if ( string . IsNullOrEmpty ( token ) || element == null ) return ;
91-
92- if ( ContainerDict . ContainsKey ( token ) )
93- {
94- if ( ReferenceEquals ( ContainerDict [ token ] , element ) )
95- {
96- ContainerDict . Remove ( token ) ;
97- }
98- }
99- }
100-
101- public static void Unregister ( FrameworkElement element )
102- {
103- if ( element == null ) return ;
104- var first = ContainerDict . FirstOrDefault ( item => ReferenceEquals ( element , item . Value ) ) ;
105- if ( ! string . IsNullOrEmpty ( first . Key ) )
106- {
107- ContainerDict . Remove ( first . Key ) ;
108- }
109- }
110-
111- public static void Unregister ( string token )
112- {
113- if ( string . IsNullOrEmpty ( token ) ) return ;
114-
115- if ( ContainerDict . ContainsKey ( token ) )
116- {
117- ContainerDict . Remove ( token ) ;
118- }
119- }
120-
12167 public static Dialog Show < T > ( string token = "" ) where T : new ( ) => Show ( new T ( ) , token ) ;
12268
12369 public static Dialog Show ( object content , string token = "" )
@@ -140,10 +86,10 @@ public static Dialog Show(object content, string token = "")
14086 {
14187 Close ( token ) ;
14288 DialogDict [ token ] = dialog ;
143- ContainerDict . TryGetValue ( token , out element ) ;
144- decorator = element is System . Windows . Window ?
145- VisualHelper . GetChild < AdornerDecorator > ( element ) :
146- VisualHelper . GetChild < DialogContainer > ( element ) ;
89+ TokenManager . TryGetControl ( token , out element ) ;
90+ decorator = element is System . Windows . Window
91+ ? VisualHelper . GetChild < AdornerDecorator > ( element )
92+ : VisualHelper . GetChild < DialogContainer > ( element ) ;
14793 }
14894
14995 if ( decorator != null )
@@ -183,7 +129,7 @@ public void Close()
183129 {
184130 Close ( WindowHelper . GetActiveWindow ( ) ) ;
185131 }
186- else if ( ContainerDict . TryGetValue ( _token , out var element ) )
132+ else if ( TokenManager . TryGetControl ( _token , out var element ) )
187133 {
188134 Close ( element ) ;
189135 DialogDict . Remove ( _token ) ;
@@ -216,6 +162,7 @@ private void Close(DependencyObject element)
216162 {
217163 decorator . Child . IsEnabled = true ;
218164 }
165+
219166 var layer = decorator . AdornerLayer ;
220167 layer ? . Remove ( _container ) ;
221168 IsClosed = true ;
0 commit comments