x:Key
behaves like an attached property where it is available in the children of ResourceDictionary
. However, I cannot find the implementation of x:Key
in ResourceDictionary
(the repo).
Question: How and where is x:Key
implemented in MAUI?
CodePudding user response:
x:Key
, along with other attributes that are part of the xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
namespace are special XmlNames, that are built into the XAML parser; they are not implemented as attached properties. Basically, they are almost equivalent to keywords in C#.
You can view the static variable declarations for them here, inside the XmlName
struct: https://github.com/dotnet/maui/blob/main/src/Controls/src/Xaml/XmlName.cs
For instance, the static XmlName
field 'xKey' (x:Key
) is referenced here in the XamlParser class: https://github.com/dotnet/maui/blob/main/src/Controls/src/Xaml/XamlParser.cs#L261
The other special XAML attributes that are implemented in the same way are:
x:Arguments
, x:DataType
, x:FactoryMethod
, x:Name
, x:TypeArguments
.
x:Class
and x:FieldModifier
are also part of the x:
namespace, but they're kind of a special case.
Edit: Should mention that if you want to create your own set of extensions that behave similarly, you can create what's called a Markup Extension. Though you can't directly add to the x:
namespace. x:Static
for instance is implemented as a markup extension: https://github.com/xamarin/Xamarin.Forms/blob/master/Xamarin.Forms.Xaml/MarkupExtensions/StaticExtension.cs
CodePudding user response:
If you actually read the XF documents it explains to you how this works:https://learn.microsoft.com/en-us/xamarin/xamarin-forms/xaml/namespaces
XAML uses prefixes to declare non-default namespaces, with the prefix being used when referencing types within the namespace. The x namespace declaration specifies that elements defined within the XAML with a prefix of x are used for elements and attributes that are intrinsic to XAML (specifically the 2009 XAML specification).
Now the Key class is defined under the 2009 XAML spec, which is probably not open source and hence is not available in repo's...
Hope this helps