【问题标题】:How to define a Color from a string constant如何从字符串常量定义颜色
【发布时间】:2018-07-14 00:59:01
【问题描述】:

我有一个静态类,其中包含静态定义的 HEX 颜色值:

namespace Namespace1
{
    public static class MyColors
    {
        public const string COLOR_1 = "#AABBCC";
        // ...
    }
}

我想在资源字典中定义所有这些颜色,以便它们可以在 XAML 中使用:

<ResourceDictionary xmlns="http://xamarin.com/schemas/2014/forms"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    x:Class="Namespace2.Style.Colors"
    xmlns:n1="clr-namespace:Namespace1;assembly=Assembly1">
    <Color x:Key="Color1" x:FactoryMethod="FromHex">
        <x:Arguments>
            <x:String>{x:Static n1:MyColors.COLOR_1}</x:String>
        </x:Arguments>
    </Color>
    <!-- ... -->
</ResourceDictionary>

但这似乎不起作用。没有编译错误,但是颜色本身是空的(透明)。

如果我将{x:Static n1:MyColors.COLOR_1} 部分替换为实际的#AABBCC 值,那么它可以工作。

我还测试了{x:Static n1:MyColors.COLOR_1} 部分(带有 Label 和 Text 属性),它可以工作。

那么这里的问题是什么?这似乎是一个内部 Xamarin.Forms 错误。

【问题讨论】:

  • 为什么要在类上定义颜色。我会在 xaml 中定义它...
  • 因为颜色常量是在通用 .NETStandard 2.0 程序集中定义的,并且 WPF 应用程序也引用了这些常量,而不仅仅是 Xamarin.Forms。
  • 你能定义两者吗?作为字符串的值和作为颜色的值?像这样的东西:stackoverflow.com/questions/10062376/…
  • 我认为不可能按照您想要的方式进行。以前有人问过。 forums.xamarin.com/discussion/65684/…
  • 哇,我还没有在论坛上找到这个讨论。那么替代方案是什么?我可以在代码中定义实际颜色,然后在 XAML 中静态引用它吗?

标签: c# xaml xamarin.forms


【解决方案1】:

问题本身并没有解决,但找到了我试图实现的解决方案:

因此,可以使用Add 方法在Colors.xaml.cs 文件中声明颜色,而不是在Colors.xaml 文件中声明颜色:

namespace Namespace2.Style
{
    public partial class Colors : ResourceDictionary
    {
        public Colors()
        {
            InitializeComponent();

            Add("Color1", Color.FromHex(MyColors.COLOR_1));
            // ...
        }
    }
}

【讨论】:

  • 好吧,为什么不呢。不过,类型转换器可能是完成它的好方法
  • 你能告诉我如何使用转换器吗?如果你这样做,我会接受你的回答,因为我肯定更喜欢 XAML 解决方案。
【解决方案2】:

好吧,忘记类型转换器。我让它像这样工作:

<SolidColorBrush x:Key="Color1" Color="{Binding Source={x:Static n1:MyColors.COLOR_1}}" />

这是你的替代品吗?

【讨论】:

  • 这就是我在 WPF 中的做法,但这个问题是关于 Xamarin.Forms 的,其中 SolidColorBrush 不存在。
  • 那么,我会坚持你的解决方案
【解决方案3】:

名为MyAssembly 的程序集中的类:

namespace MyNamespace
{
  public static class MyColors
  {
    public const string Blue = "#AABBCC";
  }

  public static class MyColor
  {
    public static Color MyBlue { get { return Color.FromHex(MyColors.Blue); } }
  }
}

XAML:

xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:c="clr-namespace:MyNamespace;assembly=MyAssembly"
...
<Label Text="MyColor.MyBlue example" TextColor="{x:Static c:MyColor.MyBlue}"/>

请注意,c 命名空间可能被命名为例如myColor 以便更好地识别它。 Label 中的符号将是 myColor:MyColor.MyBlue

【讨论】:

  • 这可行,但我更喜欢我的解决方案,在那里我可以简单地使用颜色作为StaticResource,而无需导入命名空间等。
【解决方案4】:

如果您有时间使用类型转换器测试解决方案:

namespace Namespace1
{
    using System.ComponentModel;
    using System.Windows.Media;

    public static class MyColors
    {
        [TypeConverter(typeof(ColorConverter))]
        public static string COLOR_1 { get; } = "#AABBCC";
    }
}

【讨论】:

  • 我现在如何使用它来定义 XAML 中的颜色?只这样做没有任何作用。
  • 嗯,它应该可以自动使用原始代码。所以我需要自己尝试一下,并检查为什么在这种情况下不调用转换器。如果我让它工作,我会回复你。我现在很好奇;-)
  • 它需要一个字符串,而不是颜色。当您使用此字符串字段并且预期颜色时使用转换器。但就像我说的,在这种情况下,需要一个字符串。
猜你喜欢
  • 1970-01-01
  • 2016-01-04
  • 2023-03-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多