VisualElement.Resources 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>
  <p>Note that the above snippet is only valid when nested within a <code>&lt;ContentPage&gt;...&lt;/ContentPage&gt;</code> tag pair. Within that pair, the app developer can use both of the <code>horzOptions</code> and <code>vertOptions</code> keys to specify values for properties of type <xref data-throw-if-not-resolved="true" uid="Xamarin.Forms.LayoutOptions"></xref> by using the <code>"{...}"</code> static resource syntax. The short example below, also taken from  the <format type="text/html"><a href="http://developer.xamarin.com/guides/cross-platform/xamarin-forms/xaml-for-xamarin-forms/">XAML for Xamarin.Forms</a></format> series, illustrates this syntax:</p>
  <pre><code>

<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