Xaml Style and Resource warnings

[XR5005, XR5006, XR5064] ResourceDictionary and MergedDictionary declaration errors

Xaml Resources (e.g. Colors, Fonts, Styles) are usually declared within the Resources element of a wrapper layout. The Resources tag should be prefixed by the name of the parent layout. For instance when declaring Resources within a StackLayout the Resources tag should be “<StackLayout.Resources>”. The elements within the <[Layout].Resources> and <ResourceDictionary.MergedDictionary> tags also need to be wrapped in a ResourceDictionary for specific platforms and versions. XamRight warns if it can’t locate a ResourceDictionary when it is necessary.

[XR5052] Duplicate Resource Key in ResourceDictionary

All the x:Key values in a ResourceDictionary has to be unique. XamRight warns if it detects more than one x:Key with the same value.

[XR5049, XR5050, XR5056, XR5070] Error in Style element declaration in ResourceDictionary

When declaring styles in a ResourceDictionary, the Style element needs to contain a TargetType attribute that indicates the type of element the Style is used for (except for WPF where the TargetType can be dropped). The x:Key attribute on the Style tag also cannot be set to a string that starts or ends with whitespace. Apart from that, the x:Name attribute should not be used as a substitute for x:Key as the usage of x:Key is preferred over x:Name as specified here: https://docs.microsoft.com/en-us/dotnet/framework/xaml-services/x-name-directive. Accordingly, XamRight checks for these syntax errors.

[XR5014, XR5015] Error in TargetType declaration on Style Element

As noted above the a Style element has to declare a TargetType. Additionally, the TargetType attribute has to be set to a type that derives Element for Xamarin, FrameworkElement for UWP, and FrameworkElement or FrameworkContentElement for WPF. XamRight warns if the TargetType attribute is not declared or is set to a type that does not exists or derive the correct base type.

[XR5045] Style applied to element that does not match TargetType

A Style can be applied to a UI element by setting the element’s Style attribute to a StaticResource markup that references a Style defined in a ResourceDictionary. If the type of the element or any of its base types do not match the TargetType set on the Style, XamRight will issue a warning.

[XR5053] Multiple implicit styles for the same TargetType

If a Style only declares a TargetType (without declaring x:Key) that means the Style will be applied to all of the elements (within its scope) that match the TargetType. Hence, if a ResourceDictionary has more than one Style defined with only a TargetType set to the same value, there would be competing Style values applied to the same UI element. This will cause runtime errors, and is caught by XamRight.

[XR5057, XR5058] Property setters on Style do not match properties on TargetType or target UI node

Each of the property setters within a Style’s tag has to correspond to a property on the Style’s TargetType. XamRight first does this check and later checks if the node the Style is applied to contains all the properties defined as setters within the Style tag. XamRight warns if it cannot locate any of the property setters on the nodes where the Style is applied.

Author