|
1 | 1 | using Microsoft.Extensions.Logging; |
2 | 2 | using Microsoft.Extensions.Logging.Abstractions; |
3 | 3 | using Microsoft.UI.Xaml; |
| 4 | +using Microsoft.UI.Xaml.Controls.Primitives; |
4 | 5 | using System; |
5 | 6 | using System.Collections.Generic; |
6 | 7 | using System.Linq; |
7 | 8 | using System.Reflection; |
8 | 9 | using System.Threading.Tasks; |
| 10 | +using System.Xml.Linq; |
9 | 11 |
|
10 | 12 | namespace WinUI3Localizer; |
11 | 13 |
|
@@ -242,17 +244,46 @@ private static void DependencyObjectsReferences_DependencyObjectRemoved(object? |
242 | 244 | { |
243 | 245 | return property; |
244 | 246 | } |
245 | | - else if (type.GetField( |
| 247 | + |
| 248 | + if (type.GetField( |
246 | 249 | dependencyPropertyName, |
247 | 250 | BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy) is FieldInfo fieldInfo && |
248 | 251 | fieldInfo.GetValue(null) is DependencyProperty field) |
249 | 252 | { |
250 | 253 | return field; |
251 | 254 | } |
252 | 255 |
|
| 256 | + // TODO: This should be done on the building process. |
| 257 | + if (dependencyPropertyName.Split(".") is string[] splitResult && |
| 258 | + splitResult.Length is 2) |
| 259 | + { |
| 260 | + string attachedPropertyClassName = splitResult[0]; |
| 261 | + IEnumerable<Type> types = GetTypesFromName(attachedPropertyClassName); |
| 262 | + |
| 263 | + string attachedPropertyName = splitResult[1]; |
| 264 | + IEnumerable<PropertyInfo> attachedProperties = types |
| 265 | + .Select(x => x.GetProperty(attachedPropertyName)) |
| 266 | + .OfType<PropertyInfo>(); |
| 267 | + |
| 268 | + foreach (PropertyInfo attachedProperty in attachedProperties) |
| 269 | + { |
| 270 | + if (attachedProperty.GetValue(null) is DependencyProperty dependencyProperty) |
| 271 | + { |
| 272 | + return dependencyProperty; |
| 273 | + } |
| 274 | + } |
| 275 | + } |
| 276 | + |
253 | 277 | return null; |
254 | 278 | } |
255 | 279 |
|
| 280 | + private static IEnumerable<Type> GetTypesFromName(string name) |
| 281 | + { |
| 282 | + return AppDomain.CurrentDomain.GetAssemblies() |
| 283 | + .SelectMany(x => x.GetTypes()) |
| 284 | + .Where(x => x.Name == name); |
| 285 | + } |
| 286 | + |
256 | 287 | private async Task LocalizeDependencyObjects() |
257 | 288 | { |
258 | 289 | foreach (DependencyObject dependencyObject in await this.dependencyObjectsReferences.GetDependencyObjects()) |
@@ -280,11 +311,10 @@ private void LocalizeDependencyObject(DependencyObject dependencyObject, Languag |
280 | 311 | item.DependencyPropertyName) is DependencyProperty dependencyProperty) |
281 | 312 | { |
282 | 313 | LocalizeDependencyObjectsWithDependencyProperty(dependencyObject, dependencyProperty, item.Value); |
| 314 | + return; |
283 | 315 | } |
284 | | - else |
285 | | - { |
286 | | - LocalizeDependencyObjectsWithoutDependencyProperty(dependencyObject, item.Value); |
287 | | - } |
| 316 | + |
| 317 | + LocalizeDependencyObjectsWithoutDependencyProperty(dependencyObject, item.Value); |
288 | 318 | } |
289 | 319 |
|
290 | 320 | private void LocalizeDependencyObjectsWithDependencyProperty(DependencyObject dependencyObject, DependencyProperty dependencyProperty, string value) |
|
0 commit comments