VisualElement.Resources 屬性
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
取得或設定本機資源字典。
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>
巢狀時,上述代碼段才有效。 在該配對中,應用程式開發人員可以使用 horzOptions
和 vertOptions
索引鍵,使用"{...}"
靜態資源語法來指定 類型LayoutOptions屬性的值。 下列簡短範例也取自適用於 Xamarin.Forms 系列的 XAML,說明下列語法:
<Button Text="Do this!"
HorizontalOptions="{StaticResource horzOptions}"
VerticalOptions="{StaticResource vertOptions}"
BorderWidth="3"
Rotation="-15"
TextColor="Red"
Font="Large" />
資源字典及其相關聯的 XML 提供應用程式開發人員方便的方法,以在 XAML 編譯時間和運行時間引擎內重複使用程式碼。