VisualElement.Resources Property

Definition

Gets or sets the local resource dictionary.

public Xamarin.Forms.ResourceDictionary Resources { get; set; }
member this.Resources : Xamarin.Forms.ResourceDictionary with get, set

Property Value

The current resource dictionary, where resources are stored by key.

Remarks

In XAML, resource dictionaries are filled with key/value pairs that are specified in XML and consequently created at run time. The keys in the resource dictionary are specified with the x:Key attribute of the XML tag for the type to create. An object of that type is created, and is initialized with the property and field values that are specified either by additional attributes or by nested tags, both of which, when present are simply string representations of the property or field names. The object is then inserted into the ResourceDictionary for the enclosing type at runtime.

For example, the XAML below, taken from the XAML for Xamarin.Forms series, creates a resource dictionary that contains LayoutOptions object constants that can be used for any Layout objects that are added to the surrounding ContentPage:

<ContentPage.Resources>
<ResourceDictionary>
<LayoutOptions x:Key="horzOptions"
                     Alignment="Center" />

<LayoutOptions x:Key="vertOptions"
                     Alignment="Center"
                     Expands="True" />
</ResourceDictionary>
</ContentPage.Resources>

Note that the above snippet is only valid when nested within a <ContentPage>...</ContentPage> tag pair. Within that pair, the app developer can use both of the horzOptions and vertOptions keys to specify values for properties of type LayoutOptions by using the "{...}" static resource syntax. The short example below, also taken from the XAML for Xamarin.Forms series, illustrates this syntax:

<Button Text="Do this!"
        HorizontalOptions="{StaticResource horzOptions}"
        VerticalOptions="{StaticResource vertOptions}"
        BorderWidth="3"
        Rotation="-15"
        TextColor="Red"
        Font="Large" />

Resource dictionaries and their associated XML provide the application developer with a convenient method to reuse code inside the XAML compile-time and run-time engines.

Applies to