-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathReactorCoreXamlMetaDataProvider.cs
More file actions
149 lines (133 loc) · 7.11 KB
/
ReactorCoreXamlMetaDataProvider.cs
File metadata and controls
149 lines (133 loc) · 7.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Markup;
using Microsoft.UI.Xaml.Media;
namespace Microsoft.UI.Reactor.Hosting;
/// <summary>
/// Hand-rolled IXamlMetadataProvider that covers system primitives, core Microsoft.UI.Xaml
/// types, common value structs, and the enums referenced by XamlControlsResources' internal
/// XAML. Needed under native AOT because the default XamlControlsXamlMetaDataProvider only
/// covers types in the Microsoft.UI.Xaml.Controls namespace. Under JIT, the XAML parser
/// falls back to reflective type resolution for unknown names; under AOT that path throws,
/// producing a STATUS_APPLICATION_INTERNAL_EXCEPTION crash inside Microsoft.UI.Xaml.dll
/// during Application bootstrap (when XamlControlsResources loads its theme dictionaries).
/// </summary>
internal sealed partial class ReactorCoreXamlMetaDataProvider : IXamlMetadataProvider
{
private static readonly (string Name, Type Type)[] s_entries =
[
// System primitives — WinUI queries these during Setter Value resolution and schema checks.
("Object", typeof(object)),
("Boolean", typeof(bool)),
("Byte", typeof(byte)),
("Int16", typeof(short)),
("Int32", typeof(int)),
("Int64", typeof(long)),
("Single", typeof(float)),
("Double", typeof(double)),
("Char", typeof(char)),
("String", typeof(string)),
("DateTime", typeof(DateTime)),
("TimeSpan", typeof(TimeSpan)),
("Guid", typeof(Guid)),
("Uri", typeof(Uri)),
// Core Microsoft.UI.Xaml — not in XamlControlsXamlMetaDataProvider because
// that one only covers Microsoft.UI.Xaml.Controls.*
("Microsoft.UI.Xaml.DependencyObject", typeof(DependencyObject)),
("Microsoft.UI.Xaml.UIElement", typeof(UIElement)),
("Microsoft.UI.Xaml.FrameworkElement", typeof(FrameworkElement)),
("Microsoft.UI.Xaml.ResourceDictionary", typeof(ResourceDictionary)),
("Microsoft.UI.Xaml.Style", typeof(Style)),
("Microsoft.UI.Xaml.Setter", typeof(Setter)),
("Microsoft.UI.Xaml.SetterBase", typeof(SetterBase)),
("Microsoft.UI.Xaml.DataTemplate", typeof(DataTemplate)),
("Microsoft.UI.Xaml.FrameworkTemplate", typeof(FrameworkTemplate)),
// Enums referenced by Setter values in theme dictionaries.
("Microsoft.UI.Xaml.Visibility", typeof(Visibility)),
("Microsoft.UI.Xaml.HorizontalAlignment", typeof(HorizontalAlignment)),
("Microsoft.UI.Xaml.VerticalAlignment", typeof(VerticalAlignment)),
("Microsoft.UI.Xaml.TextAlignment", typeof(TextAlignment)),
("Microsoft.UI.Xaml.TextWrapping", typeof(TextWrapping)),
("Microsoft.UI.Xaml.TextTrimming", typeof(TextTrimming)),
("Microsoft.UI.Xaml.FlowDirection", typeof(FlowDirection)),
("Microsoft.UI.Xaml.GridUnitType", typeof(GridUnitType)),
("Microsoft.UI.Xaml.Controls.Orientation",typeof(Orientation)),
("Microsoft.UI.Xaml.Controls.ControlTemplate", typeof(ControlTemplate)),
// Structs serialized in XAML attribute form.
("Microsoft.UI.Xaml.Thickness", typeof(Thickness)),
("Microsoft.UI.Xaml.CornerRadius", typeof(CornerRadius)),
("Microsoft.UI.Xaml.GridLength", typeof(GridLength)),
("Microsoft.UI.Xaml.Duration", typeof(Duration)),
// Media primitives.
("Microsoft.UI.Xaml.Media.Brush", typeof(Brush)),
("Microsoft.UI.Xaml.Media.SolidColorBrush", typeof(SolidColorBrush)),
// Windows namespace structs used in XAML.
("Windows.UI.Color", typeof(global::Windows.UI.Color)),
("Windows.Foundation.Size", typeof(global::Windows.Foundation.Size)),
("Windows.Foundation.Point", typeof(global::Windows.Foundation.Point)),
("Windows.Foundation.Rect", typeof(global::Windows.Foundation.Rect)),
];
private static readonly Dictionary<string, Type> s_byName = BuildNameMap();
private static readonly Dictionary<Type, string> s_byType = BuildTypeMap();
private static Dictionary<string, Type> BuildNameMap()
{
var map = new Dictionary<string, Type>(s_entries.Length, StringComparer.Ordinal);
foreach (var (name, type) in s_entries)
map[name] = type;
return map;
}
private static Dictionary<Type, string> BuildTypeMap()
{
var map = new Dictionary<Type, string>(s_entries.Length);
foreach (var (name, type) in s_entries)
map[type] = name;
return map;
}
public IXamlType? GetXamlType(Type type)
=> s_byType.TryGetValue(type, out var name) ? new CoreXamlType(name, type) : null;
public IXamlType? GetXamlType(string fullName)
=> s_byName.TryGetValue(fullName, out var type) ? new CoreXamlType(fullName, type) : null;
public XmlnsDefinition[] GetXmlnsDefinitions() => [];
/// <summary>
/// Minimal IXamlType that satisfies schema-level lookups. WinUI's XAML loader calls
/// GetXamlType during parsing to verify that types referenced in XAML exist; for system
/// and schema-only types, returning a non-null stub with correct FullName + UnderlyingType
/// is sufficient. Activation and member access are unreachable for these types because
/// Reactor apps do not construct them from XAML markup — they only appear as schema
/// references inside the WinUI theme dictionaries.
/// </summary>
private sealed partial class CoreXamlType : IXamlType
{
public CoreXamlType(string fullName, Type underlyingType)
{
FullName = fullName;
UnderlyingType = underlyingType;
}
public string FullName { get; }
public Type UnderlyingType { get; }
public IXamlType? BaseType => null;
public IXamlMember? ContentProperty => null;
public bool IsArray => false;
public bool IsCollection => false;
public bool IsConstructible => false;
public bool IsDictionary => false;
public bool IsMarkupExtension => false;
public bool IsBindable => false;
public bool IsReturnTypeStub => false;
public bool IsLocalType => false;
public IXamlType? ItemType => null;
public IXamlType? KeyType => null;
public IXamlType? BoxedType => null;
public IXamlMember? GetMember(string name) => null;
public object ActivateInstance() => throw new NotSupportedException($"{FullName} is schema-only; cannot activate from XAML.");
public void AddToMap(object instance, object key, object item) => throw new NotSupportedException();
public void AddToVector(object instance, object item) => throw new NotSupportedException();
public void RunInitializer() { }
public object CreateFromString(string input)
{
if (UnderlyingType.IsEnum)
return Enum.Parse(UnderlyingType, input, ignoreCase: true);
throw new NotSupportedException($"Cannot parse '{input}' for schema-only type {FullName}.");
}
}
}