Skip to content

Commit cae8034

Browse files
Add AttachedProperties localization
This should be done on the building process.
1 parent 2f16490 commit cae8034

File tree

1 file changed

+35
-5
lines changed

1 file changed

+35
-5
lines changed

WinUI3Localizer/Localizer.cs

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
using Microsoft.Extensions.Logging;
22
using Microsoft.Extensions.Logging.Abstractions;
33
using Microsoft.UI.Xaml;
4+
using Microsoft.UI.Xaml.Controls.Primitives;
45
using System;
56
using System.Collections.Generic;
67
using System.Linq;
78
using System.Reflection;
89
using System.Threading.Tasks;
10+
using System.Xml.Linq;
911

1012
namespace WinUI3Localizer;
1113

@@ -242,17 +244,46 @@ private static void DependencyObjectsReferences_DependencyObjectRemoved(object?
242244
{
243245
return property;
244246
}
245-
else if (type.GetField(
247+
248+
if (type.GetField(
246249
dependencyPropertyName,
247250
BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy) is FieldInfo fieldInfo &&
248251
fieldInfo.GetValue(null) is DependencyProperty field)
249252
{
250253
return field;
251254
}
252255

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+
253277
return null;
254278
}
255279

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+
256287
private async Task LocalizeDependencyObjects()
257288
{
258289
foreach (DependencyObject dependencyObject in await this.dependencyObjectsReferences.GetDependencyObjects())
@@ -280,11 +311,10 @@ private void LocalizeDependencyObject(DependencyObject dependencyObject, Languag
280311
item.DependencyPropertyName) is DependencyProperty dependencyProperty)
281312
{
282313
LocalizeDependencyObjectsWithDependencyProperty(dependencyObject, dependencyProperty, item.Value);
314+
return;
283315
}
284-
else
285-
{
286-
LocalizeDependencyObjectsWithoutDependencyProperty(dependencyObject, item.Value);
287-
}
316+
317+
LocalizeDependencyObjectsWithoutDependencyProperty(dependencyObject, item.Value);
288318
}
289319

290320
private void LocalizeDependencyObjectsWithDependencyProperty(DependencyObject dependencyObject, DependencyProperty dependencyProperty, string value)

0 commit comments

Comments
 (0)