ResourceDictionary
是應用程式所 Xamarin.Forms 使用資源的存放庫。 儲存在 中的
ResourceDictionary
一般資源包括
樣式
、
控制項範本
、
資料範本
、色彩和轉換器。
在 XAML 中,儲存在 中的
ResourceDictionary
資源可以使用 或
DynamicResource
標記延伸來參考並套用至專案
StaticResource
。 在 C# 中,資源也可以在 中
ResourceDictionary
定義,然後使用以字串為基礎的索引子參考並套用至元素。 不過,在 C# 中使用
ResourceDictionary
沒有好處,因為共用物件可以儲存為欄位或屬性,而且直接存取,而不需要先從字典擷取它們。
在 XAML 中建立資源
每個
VisualElement
衍生物件都有 屬性
Resources
,也就是
ResourceDictionary
可以包含資源的 。 同樣地,
Application
衍生物件具有
Resources
屬性,也就是
ResourceDictionary
可以包含資源的 。
Xamarin.Forms應用程式只包含衍生自
Application
的類別,但通常會使用衍生自
VisualElement
的許多類別,包括頁面、版面配置和控制項。 其中任何一個
ResourceDictionary
物件都可以將其
Resources
屬性設定為包含的資源。 選擇要在何處放置資源可使用的特定
ResourceDictionary
影響:
ResourceDictionary
中附加至檢視的資源,例如
Button
或
Label
,只能套用至該特定物件。
ResourceDictionary
附加至配置的資源,例如
StackLayout
或
Grid
可以套用至配置和該版面配置的所有子系。
ResourceDictionary
在頁面層級定義的資源可以套用至頁面及其所有子系。
定義于應用層級的資源
ResourceDictionary
可以在整個應用程式中套用。
除了隱含樣式之外,資源字典中的每個資源都必須具有以
x:Key
屬性定義的唯一字串索引鍵。
下列 XAML 顯示
App.xaml
檔案應用層級
ResourceDictionary
中定義的資源:
<Application xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="ResourceDictionaryDemo.App">
<Application.Resources>
<Thickness x:Key="PageMargin">20</Thickness>
<!-- Colors -->
<Color x:Key="AppBackgroundColor">AliceBlue</Color>
<Color x:Key="NavigationBarColor">#1976D2</Color>
<Color x:Key="NavigationBarTextColor">White</Color>
<Color x:Key="NormalTextColor">Black</Color>
<!-- Implicit styles -->
<Style TargetType="{x:Type NavigationPage}">
<Setter Property="BarBackgroundColor"
Value="{StaticResource NavigationBarColor}" />
<Setter Property="BarTextColor"
Value="{StaticResource NavigationBarTextColor}" />
</Style>
<Style TargetType="{x:Type ContentPage}"
ApplyToDerivedTypes="True">
<Setter Property="BackgroundColor"
Value="{StaticResource AppBackgroundColor}" />
</Style>
</Application.Resources>
</Application>
在此範例中,資源字典會定義資源、多個 Color 資源,以及兩個 Thickness 隱含 Style 資源。 如需 類別 App 的詳細資訊,請參閱Xamarin.Forms 應用程式類別。
在明確 ResourceDictionary 標記之間放置所有資源也是有效的。 不過,由於 Xamarin.Forms 3.0, ResourceDictionary 因此不需要標記。 相反地, ResourceDictionary 物件會自動建立,而且您可以直接在 Resources property-element 標記之間插入資源。
在 XAML 中取用資源
每個資源都有使用 x:Key 屬性指定的索引鍵,這會成為 中的 ResourceDictionary 字典索引鍵。 索引鍵是用來從 ResourceDictionary 參考具有 或 DynamicResource 標記延伸的資源 StaticResource 。
標記 StaticResource 延伸類似于 DynamicResource 中的標記延伸,兩者都使用字典索引鍵從資源字典參考值。 不過,雖然 StaticResource 標記延伸會執行單一字典查閱,但 DynamicResource 標記延伸會維護字典索引鍵的連結。 因此,如果取代與索引鍵相關聯的字典專案,則會將變更套用至視覺專案。 這可讓應用程式進行執行時間資源變更。 如需標記延伸的詳細資訊,請參閱 XAML 標記延伸。
下列 XAML 範例示範如何取用資源,並在 中 StackLayout 定義其他資源:
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="ResourceDictionaryDemo.HomePage"
Title="Home Page">
<StackLayout Margin="{StaticResource PageMargin}">
<StackLayout.Resources>
<!-- Implicit style -->
<Style TargetType="Button">
<Setter Property="FontSize" Value="Medium" />
<Setter Property="BackgroundColor" Value="#1976D2" />
<Setter Property="TextColor" Value="White" />
<Setter Property="CornerRadius" Value="5" />
</Style>
</StackLayout.Resources>
<Label Text="This app demonstrates consuming resources that have been defined in resource dictionaries." />
<Button Text="Navigate"
Clicked="OnNavigateButtonClicked" />
</StackLayout>
</ContentPage>
在此範例中 ContentPage ,物件會取用應用層級資源字典中定義的隱含樣式。 物件 StackLayout 會 PageMargin 取用應用層級資源字典中定義的資源,而 Button 物件則會取用資源字典中 StackLayout 定義的隱含樣式。 這會導致下列螢幕擷取畫面中顯示的外觀:
單一頁面特有的資源不應包含在應用層級資源字典中,因為這類資源會在應用程式啟動時剖析,而不是頁面需要時。 如需詳細資訊,請參閱 減少應用程式資源字典大小。
資源查閱行為
使用 或 DynamicResource 標記延伸參考 StaticResource 資源時,會發生下列查閱程式:
如果資源字典存在,則會針對設定 屬性的 元素檢查所要求的索引鍵。 如果找到要求的索引鍵,則會傳回其值,且查閱程式會終止。
如果找不到相符專案,查閱程式會向上搜尋視覺化樹狀結構,並檢查每個父元素的資源字典。 如果找到要求的索引鍵,則會傳回其值,且查閱程式會終止。 否則,進程會往上繼續,直到到達根項目為止。
如果在根項目上找不到相符專案,則會檢查應用層級資源字典。
如果仍然找不到相符專案, XamlParseException 則會擲回 。
因此,當 XAML 剖析器遇到 StaticResource 或 DynamicResource 標記延伸時,它會使用找到的第一個相符專案,透過視覺化樹狀結構向上移動來搜尋相符的索引鍵。 如果此搜尋結束于頁面,而且索引鍵仍然找不到,XAML 剖析器就會搜尋 ResourceDictionary 附加至 App 物件的 。 如果仍然找不到索引鍵,則會擲回例外狀況。
當資源分享索引鍵時,在視覺化樹狀結構中定義較低的資源會優先于較高定義的資源。 例如,將資源設定 AppBackgroundColor 為 AliceBlue 應用層級,將會由頁面層級 AppBackgroundColor 資源設定為 Teal 來覆寫。 同樣地,控制層級資源會覆寫頁面層級 AppBackgroundColorAppBackgroundColor 資源。
獨立資源字典
衍生自 ResourceDictionary 的類別也可以位於獨立 XAML 檔案中。 XAML 檔案接著可以在應用程式之間共用。
若要建立這類檔案,請將新的 內容檢視 或 內容頁面 專案新增至專案 (,但不包含只有 C# 檔案) 的內容檢視 或 內容頁面 。 刪除程式碼後置檔案,並在 XAML 檔案中,將基類的名稱從 ContentView 或 ContentPage 變更為 ResourceDictionary 。 此外,請從檔案的根標記中移除 x:Class 屬性。
下列 XAML 範例顯示 ResourceDictionary 名為 MyResourceDictionary.xaml:
<ResourceDictionary xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml">
<DataTemplate x:Key="PersonDataTemplate">
<ViewCell>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="0.5*" />
<ColumnDefinition Width="0.2*" />
<ColumnDefinition Width="0.3*" />
</Grid.ColumnDefinitions>
<Label Text="{Binding Name}"
TextColor="{StaticResource NormalTextColor}"
FontAttributes="Bold" />
<Label Grid.Column="1"
Text="{Binding Age}"
TextColor="{StaticResource NormalTextColor}" />
<Label Grid.Column="2"
Text="{Binding Location}"
TextColor="{StaticResource NormalTextColor}"
HorizontalTextAlignment="End" />
</Grid>
</ViewCell>
</DataTemplate>
</ResourceDictionary>
在此範例中 ResourceDictionary ,包含單一資源,這是 類型 DataTemplate 的物件。 您可以將 MyResourceDictionary.xaml 合併至另一個資源字典來取用。
根據預設,連結器會在連結器行為設定為連結所有元件時,從發行組建中移除獨立 XAML 檔案。 為了確保獨立 XAML 檔案會保留在發行組建中:
將自訂 Preserve 屬性新增至包含獨立 XAML 檔案的元件。 如需詳細資訊,請參閱 保留程式碼。
在 Preserve 元件層級設定 屬性:
[assembly:Preserve(AllMembers = true)]
如需連結的詳細資訊,請參閱 連結 Xamarin.iOS 應用程式和Android 上的連結。
合併的資源字典
合併的資源字典會將一或多個 ResourceDictionary 物件合併到另一個 ResourceDictionary 。
合併本機資源字典
ResourceDictionary本機檔案可以藉由建立 屬性設定為具有資源的 XAML 檔案檔案名的物件 Source ,以合併到另一個 ResourceDictionaryResourceDictionary 物件:
<ContentPage ...>
<ContentPage.Resources>
<!-- Add more resources here -->
<ResourceDictionary Source="MyResourceDictionary.xaml" />
<!-- Add more resources here -->
</ContentPage.Resources>
</ContentPage>
此語法不會具現化 MyResourceDictionary 類別。 相反地,它會參考 XAML 檔案。 因此,設定 屬性時 Source 不需要程式碼後置檔案,而且 x:Class 可以從 MyResourceDictionary.xaml 檔案的根標籤中移除屬性。
屬性 Source 只能從 XAML 設定。
從其他元件合併資源字典
ResourceDictionary也可以藉由將它加入 至 的 ResourceDictionary 屬性,將其合併到另 MergedDictionaries 一個 ResourceDictionary 。 不論資源字典所在的元件為何,這項技術都允許合併資源字典。 從外部元件合併資源字典需要 ResourceDictionary 將建置動作設定為 EmbeddedResource、具有程式碼後置檔案,以及在檔案的根標籤中定義 x:Class 屬性。
ResourceDictionary 類別也會定義 MergedWith 屬性。 不過,這個屬性已被取代,不應再使用。
下列程式碼範例顯示將兩個資源字典新增至 MergedDictionaries 頁面層級 ResourceDictionary 的集合:
<ContentPage ...
xmlns:local="clr-namespace:ResourceDictionaryDemo"
xmlns:theme="clr-namespace:MyThemes;assembly=MyThemes">
<ContentPage.Resources>
<ResourceDictionary>
<!-- Add more resources here -->
<ResourceDictionary.MergedDictionaries>
<!-- Add more resource dictionaries here -->
<local:MyResourceDictionary />
<theme:LightTheme />
<!-- Add more resource dictionaries here -->
</ResourceDictionary.MergedDictionaries>
<!-- Add more resources here -->
</ResourceDictionary>
</ContentPage.Resources>
</ContentPage>
在此範例中,來自相同元件的資源字典,以及外部元件的資源字典會合並到頁面層級資源字典中。 此外,您也可以在屬性元素標籤內 MergedDictionaries 新增其他 ResourceDictionary 物件,以及這些標記以外的其他資源。
中只能有一個 MergedDictionariesResourceDictionary 屬性元素標記,但您可以視需要將許多 ResourceDictionary 物件放在該處。
合併 ResourceDictionary 的資源分享相同的 x:Key 屬性值時, Xamarin.Forms 會使用下列資源優先順序:
資源字典的本機資源。
透過集合合併 MergedDictionaries 之資源字典中包含的資源,會以相反順序列出于 屬性中 MergedDictionaries 。
如果應用程式包含多個大型資源字典,搜尋資源字典可以是計算密集型工作。 因此,為了避免不必要的搜尋,您應該確保應用程式中的每個頁面只會使用適合頁面的資源字典。
資源字典 (範例)
XAML 標記延伸
Xamarin.Forms 風格
連結 Xamarin.iOS 應用程式
在 Android 上連結
ResourceDictionary API
在 Channel 9 與 YouTube 上尋找更多 Xamarin 影片。