Skip to content

Commit 5de152b

Browse files
committed
fix: build error.
1 parent 6f72c2f commit 5de152b

File tree

1 file changed

+119
-9
lines changed

1 file changed

+119
-9
lines changed

src/Shared/HandyControl_Shared/Tools/Helper/ControlTokenManager`1.cs

Lines changed: 119 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ internal class ControlTokenManager<T>(
1010
)
1111
where T : FrameworkElement
1212
{
13-
private static readonly Dictionary<string, WeakReference<T>> ControlDict = new();
13+
#if NET40
14+
private readonly Dictionary<string, WeakReference> _controlDict = new();
1415

1516
public void Register(string token, T control)
1617
{
@@ -19,7 +20,7 @@ public void Register(string token, T control)
1920
return;
2021
}
2122

22-
ControlDict[token] = new WeakReference<T>(control);
23+
_controlDict[token] = new WeakReference(control);
2324
registerCallback?.Invoke(token, control);
2425
}
2526

@@ -30,7 +31,115 @@ public void Unregister(string token, T control)
3031
return;
3132
}
3233

33-
if (!ControlDict.TryGetValue(token, out var reference))
34+
if (!_controlDict.TryGetValue(token, out var reference))
35+
{
36+
return;
37+
}
38+
39+
if (!ReferenceEquals(reference.Target, control))
40+
{
41+
return;
42+
}
43+
44+
_controlDict.Remove(token);
45+
unregisterCallback?.Invoke(token, control);
46+
}
47+
48+
public void Unregister(T control)
49+
{
50+
if (control == null)
51+
{
52+
return;
53+
}
54+
55+
string unregisteredToken = null;
56+
foreach (var item in _controlDict)
57+
{
58+
if (!ReferenceEquals(control, item.Value.Target))
59+
{
60+
continue;
61+
}
62+
63+
unregisteredToken = item.Key;
64+
break;
65+
}
66+
67+
if (unregisteredToken == null)
68+
{
69+
return;
70+
}
71+
72+
_controlDict.Remove(unregisteredToken);
73+
unregisterCallback?.Invoke(unregisteredToken, control);
74+
}
75+
76+
public void Unregister(string token)
77+
{
78+
if (string.IsNullOrEmpty(token))
79+
{
80+
return;
81+
}
82+
83+
if (!_controlDict.TryGetValue(token, out var reference))
84+
{
85+
return;
86+
}
87+
88+
_controlDict.Remove(token);
89+
unregisterCallback?.Invoke(token, (T) reference.Target);
90+
}
91+
92+
public bool TryGetControl(string token, out T control)
93+
{
94+
control = null;
95+
96+
if (string.IsNullOrEmpty(token) || !_controlDict.TryGetValue(token, out var reference))
97+
{
98+
return false;
99+
}
100+
101+
control = (T) reference.Target;
102+
return true;
103+
}
104+
105+
public void OnTokenChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
106+
{
107+
if (d is not T control)
108+
{
109+
return;
110+
}
111+
112+
if (e.NewValue == null)
113+
{
114+
Unregister(control);
115+
}
116+
else
117+
{
118+
Register(e.NewValue.ToString(), control);
119+
}
120+
}
121+
#else
122+
private readonly Dictionary<string, WeakReference<T>> _controlDict = new();
123+
124+
public void Register(string token, T control)
125+
{
126+
if (string.IsNullOrEmpty(token) || control == null)
127+
{
128+
return;
129+
}
130+
131+
_controlDict[token] = new WeakReference<T>(control);
132+
registerCallback?.Invoke(token, control);
133+
}
134+
135+
public void Unregister(string token, T control)
136+
{
137+
if (string.IsNullOrEmpty(token) || control == null)
138+
{
139+
return;
140+
}
141+
142+
if (!_controlDict.TryGetValue(token, out var reference))
34143
{
35144
return;
36145
}
@@ -45,7 +154,7 @@ public void Unregister(string token, T control)
45154
return;
46155
}
47156

48-
ControlDict.Remove(token);
157+
_controlDict.Remove(token);
49158
unregisterCallback?.Invoke(token, control);
50159
}
51160

@@ -57,7 +166,7 @@ public void Unregister(T control)
57166
}
58167

59168
string unregisteredToken = null;
60-
foreach (var item in ControlDict)
169+
foreach (var item in _controlDict)
61170
{
62171
if (!item.Value.TryGetTarget(out var target) || !ReferenceEquals(control, target))
63172
{
@@ -73,7 +182,7 @@ public void Unregister(T control)
73182
return;
74183
}
75184

76-
ControlDict.Remove(unregisteredToken);
185+
_controlDict.Remove(unregisteredToken);
77186
unregisterCallback?.Invoke(unregisteredToken, control);
78187
}
79188

@@ -84,12 +193,12 @@ public void Unregister(string token)
84193
return;
85194
}
86195

87-
if (!ControlDict.TryGetValue(token, out var reference))
196+
if (!_controlDict.TryGetValue(token, out var reference))
88197
{
89198
return;
90199
}
91200

92-
ControlDict.Remove(token);
201+
_controlDict.Remove(token);
93202

94203
if (!reference.TryGetTarget(out var target))
95204
{
@@ -103,7 +212,7 @@ public bool TryGetControl(string token, out T control)
103212
{
104213
control = null;
105214

106-
if (string.IsNullOrEmpty(token) || !ControlDict.TryGetValue(token, out var reference))
215+
if (string.IsNullOrEmpty(token) || !_controlDict.TryGetValue(token, out var reference))
107216
{
108217
return false;
109218
}
@@ -127,4 +236,5 @@ public void OnTokenChanged(DependencyObject d, DependencyPropertyChangedEventArg
127236
Register(e.NewValue.ToString(), control);
128237
}
129238
}
239+
#endif
130240
}

0 commit comments

Comments
 (0)