Xamarin.Forms Guides | Samples

Xamarin.Forms.VisualElement.Resources Property

Gets or sets the local resource dictionary.

Syntax

public ResourceDictionary Resources { get; set; }

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:

Example

<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:

Example

<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.

Requirements

Namespace: Xamarin.Forms
Assembly: Xamarin.Forms.Core (in Xamarin.Forms.Core.dll)
Assembly Versions: 1.0.0.0, 1.1.0.0, 1.2.0.0, 1.3.0.0, 1.4.0.0, 1.5.0.0, 2.0.0.0