代码之家  ›  专栏  ›  技术社区  ›  Christian

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

  •  1
  • Christian  · 技术社区  · 8 年前

    @dimen/ -AndroidsXML中的常量。我发现这个未来很有用,因为它允许我轻松地改变多个应该具有相同像素长度的位置。

    3 回复  |  直到 8 年前
        1
  •  6
  •   FreakyAli    8 年前

    你在找什么 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

        2
  •  4
  •   Abdul Gani    8 年前

    除了上面提到的StaticResource之外,还有两种方法可以实现这一点。

    第一种是,为常量提供静态类,并在XAML中引用它们。

    public static class GlobalSetting
    {
        public static double ImageRotation { get { return 180; } }
    }
    

    在Xaml中,需要在page指令中添加此名称空间,

    xmlns:gb="clr-namespace:XXXX.StaticData"
    

    <Image Source="icon_back.png" Rotation="{x:Static gb:GlobalSetting.BackImageRotation}" HeightRequest="24" </Image>
    

    第二种方法是,在BaseViewModel中拥有一个常量参数,并在Xaml代码中绑定它们。

        3
  •  1
  •   FreakyAli    8 年前

    听起来您想在ResourceDictionary中定义常量/样式:

    https://docs.microsoft.com/en-us/xamarin/xamarin-forms/xaml/resource-dictionaries .

      Color={StaticResource MyColorFromDictionary}
    

    希望这有帮助!

    推荐文章