VisualElement.Resources 属性

定义

获取或设置本地资源字典。

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

属性值

当前资源字典,其中资源按键存储。

注解

在 XAML 中,资源字典填充在 XML 中指定的键/值对,因此在运行时创建。 资源字典中的键是使用 x:Key 要创建的类型的 XML 标记的 属性指定的。 创建该类型的对象,并使用附加属性或嵌套标记指定的属性和字段值进行初始化,这两个值(如果存在)只是属性或字段名称的字符串表示形式。 然后,在运行时将 对象插入 ResourceDictionary 到封闭类型的 中。

例如,下面的 XAML(取自 Xamarin.Forms 系列的 XAML)创建一个资源字典,其中包含LayoutOptions可用于添加到周围 ContentPage的任何Layout对象的对象常量:

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

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

请注意,上述代码片段仅在嵌套在标记对中 <ContentPage>...</ContentPage> 时才有效。 在该对中,应用开发人员可以使用 horzOptionsvertOptions 键,通过"{...}"静态资源语法指定类型LayoutOptions属性的值。 下面的简短示例也取自 Xamarin.Forms 系列的 XAML ,说明了此语法:

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

资源字典及其关联的 XML 为应用程序开发人员提供了一种方便的方法,用于重用 XAML 编译时引擎和运行时引擎中的代码。

适用于