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 編譯時間和執行時間引擎內重複使用程式碼。

適用於