你在找什么
ResourceDictionaries
XAML资源是可以在整个Xamarin.Forms应用程序中共享和重用的对象的定义。
这些资源对象存储在资源字典中。
ResourceDictionary
是Xamarin.Forms应用程序使用的资源的存储库。存储在ResourceDictionary中的典型资源包括样式、控制模板、数据模板、颜色和转换器。
在XAML中,存储在
然后可以使用
StaticResource
C#
然后使用基于字符串的索引器检索并应用于元素。然而,使用
在里面
,因为共享对象可以简单地存储为
fields
properties
,并直接访问,而无需首先从
dictionary
创建和使用ResourceDictionary
Resources
定义在
字典资源
然后将其设置为以下资源属性之一:
-
从应用程序派生的任何类的Resources属性。
-
从VisualElement派生的任何类的Resources属性
A.
Xamarin.Forms
Application
但通常使用从
VisualElement
,包括页面、布局和控件。这些对象中的任何一个都可以将其资源属性设置为
. 选择放置特定文件的位置
字典资源
可使用资源的影响:
-
a.资源
字典资源
Button
Label
只能应用于该特定对象,因此这不是很有用。
-
资源
在一个
附加到布局,例如
StackLayout
或
Grid
-
a.资源
在页面级别定义的可应用于页面及其所有子级。
-
a.资源
字典资源
可以在整个应用程序中应用在应用程序级别定义的。
下面的XAML显示了在应用程序级别定义的资源
字典资源
在
App.xaml
作为标准Xamarin.Forms程序的一部分创建的文件:
<Application ...>
<Application.Resources>
<ResourceDictionary>
<Color x:Key="PageBackgroundColor">Yellow</Color>
<Color x:Key="HeadingTextColor">Black</Color>
<Color x:Key="NormalTextColor">Blue</Color>
<Style x:Key="LabelPageHeadingStyle" TargetType="Label">
<Setter Property="FontAttributes" Value="Bold" />
<Setter Property="HorizontalOptions" Value="Center" />
<Setter Property="TextColor" Value="{StaticResource HeadingTextColor}" />
</Style>
</ResourceDictionary>
</Application.Resources>
Xamarin.表格
3.0,显式
字典资源
不需要标签。这个
字典资源
<Application ...>
<Application.Resources>
<Color x:Key="PageBackgroundColor">Yellow</Color>
<Color x:Key="HeadingTextColor">Black</Color>
<Color x:Key="NormalTextColor">Blue</Color>
<Style x:Key="LabelPageHeadingStyle" TargetType="Label">
<Setter Property="FontAttributes" Value="Bold" />
<Setter Property="HorizontalOptions" Value="Center" />
<Setter Property="TextColor" Value="{StaticResource HeadingTextColor}" />
</Style>
</Application.Resources>
字典资源
. 该键用于从中检索资源
字典资源
静态源
堆垛布局
:
<StackLayout Margin="0,20,0,0">
<StackLayout.Resources>
<ResourceDictionary>
<Style x:Key="LabelNormalStyle" TargetType="Label">
<Setter Property="TextColor" Value="{StaticResource NormalTextColor}" />
</Style>
<Style x:Key="MediumBoldText" TargetType="Button">
<Setter Property="FontSize" Value="Medium" />
<Setter Property="FontAttributes" Value="Bold" />
</Style>
</ResourceDictionary>
</StackLayout.Resources>
<Label Text="ResourceDictionary Demo" Style="{StaticResource LabelPageHeadingStyle}" />
<Label Text="This app demonstrates consuming resources that have been defined in resource dictionaries."
Margin="10,20,10,0"
Style="{StaticResource LabelNormalStyle}" />
<Button Text="Navigate"
Clicked="OnNavigateButtonClicked"
TextColor="{StaticResource NormalTextColor}"
Margin="0,20,0,0"
HorizontalOptions="Center"
Style="{StaticResource MediumBoldText}" />
</StackLayout>
here