gpt4 book ai didi

xaml - 如何创建要在 Xamarin.Forms XML 中使用的常量

转载 作者:行者123 更新时间:2023-12-05 08:32:07 25 4
gpt4 key购买 nike

作为一名 Android 开发人员,我习惯于使用 @dimen/ - Android XML 中的常量。我发现这个 future 很有用,因为它允许我轻松地更改多个应该具有相同像素长度的地方。

Xamarin.Forms 是否具有我可以使用的类似功能?

最佳答案

你要找的是 ResourceDictionaries

XAML resources are definitions of objects that can be shared and re-used throughout a Xamarin.Forms application. These resource objects are stored in a resource dictionary.

ResourceDictionary 是 Xamarin.Forms 应用程序使用的资源的存储库。存储在 ResourceDictionary 中的典型资源包括样式、控件模板、数据模板、颜色和转换器。

在 XAML 中,存储在 ResourceDictionary 中的资源随后可以通过使用 StaticResource 标记扩展来检索并应用于元素。在 C# 中,资源也可以在 ResourceDictionary 中定义,然后使用基于字符串的索引器检索并应用于元素。但是,在 C# 中使用 ResourceDictionary 几乎没有优势,因为共享对象可以简单地存储为 fieldsproperties,无需先从字典中检索即可直接访问。

创建和使用 ResourceDictionary

ResourcesResourceDictionary 中定义,然后将其设置为以下 Resources 属性之一:

  • 从 Application 派生的任何类的 Resources 属性。
  • 派生自 VisualElement 的任何类的 Resources 属性

Xamarin.Forms 程序仅包含一个派生自 Application 的类,但通常会使用许多派生自 VisualElement 的类,包括页面、布局和控件。这些对象中的任何一个都可以将其 Resources 属性设置为 ResourceDictionary。选择将特定 ResourceDictionary 放在何处会影响资源的使用位置:

  • ResourceDictionary 中附加到 View (例如 ButtonLabel)中的资源只能应用于该特定对象, 所以这不是很有用。

  • 可以应用 ResourceDictionary 中的
  • Resources 附加到诸如 StackLayoutGrid 的布局到布局和该布局的所有子级。

  • 在页面级别定义的 ResourceDictionary 中的资源可以应用于页面及其所有子页面。

  • 在应用程序级别定义的 ResourceDictionary 中的资源可以应用于整个应用程序。

以下 XAML 显示了在作为标准 Xamarin.Forms 程序的一部分创建的 App.xaml 文件中的应用程序级 ResourceDictionary 中定义的资源:

<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.Forms 3.0 开始,不再需要显式的 ResourceDictionary 标记。 ResourceDictionary 对象是自动创建的,您可以直接在 Resources 属性元素标签之间插入资源:

<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>

每个资源都有一个使用 x:Key 属性指定的键,它成为 ResourceDictionary 中的字典键。该键用于通过 StaticResource 标记扩展从 ResourceDictionary 中检索资源,如以下 XAML 代码示例所示,该示例显示了 StackLayout 中定义的其他资源:

<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

关于xaml - 如何创建要在 Xamarin.Forms XML 中使用的常量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53501070/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com