C# 如何从字符串常量定义颜色
我有一个静态类,具有静态定义的十六进制颜色值:C# 如何从字符串常量定义颜色,c#,xaml,xamarin.forms,C#,Xaml,Xamarin.forms,我有一个静态类,具有静态定义的十六进制颜色值: namespace Namespace1 { public static class MyColors { public const string COLOR_1 = "#AABBCC"; // ... } } 我希望在资源字典中定义所有这些颜色,以便在XAML中使用: <ResourceDictionary xmlns="http://xamarin.com/schemas/2014
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}
但这似乎不起作用。没有编译错误,但颜色本身为空(透明)
如果我将{x:Static n1:MyColors.COLOR_1}
部分替换为实际的#AABBCC
值,那么它会工作
我还测试了{x:Static n1:MyColors.COLOR_1}
部分(带有标签和文本属性),它工作正常
那么这里的问题是什么?这似乎是Xamarin.Forms的内部错误。问题本身没有解决,但我试图实现的解决方案已经找到: 因此,不必在
colors.xaml
文件中声明颜色,可以使用Add
方法在colors.xaml.cs
文件中声明颜色:
namespace Namespace2.Style
{
public partial class Colors : ResourceDictionary
{
public Colors()
{
InitializeComponent();
Add("Color1", Color.FromHex(MyColors.COLOR_1));
// ...
}
}
}
问题本身并未解决,但我试图实现的目标的解决方案已经找到: 因此,不必在
colors.xaml
文件中声明颜色,可以使用Add
方法在colors.xaml.cs
文件中声明颜色:
namespace Namespace2.Style
{
public partial class Colors : ResourceDictionary
{
public Colors()
{
InitializeComponent();
Add("Color1", Color.FromHex(MyColors.COLOR_1));
// ...
}
}
}
如果您有时间使用类型转换器测试解决方案:
namespace Namespace1
{
using System.ComponentModel;
using System.Windows.Media;
public static class MyColors
{
[TypeConverter(typeof(ColorConverter))]
public static string COLOR_1 { get; } = "#AABBCC";
}
}
如果您有时间使用类型转换器测试解决方案:
namespace Namespace1
{
using System.ComponentModel;
using System.Windows.Media;
public static class MyColors
{
[TypeConverter(typeof(ColorConverter))]
public static string COLOR_1 { get; } = "#AABBCC";
}
}
好吧,忘了类型转换器吧。我让它像这样工作:
<SolidColorBrush x:Key="Color1" Color="{Binding Source={x:Static n1:MyColors.COLOR_1}}" />
这是您的另一种选择吗?好吧,忘了类型转换器吧。我让它像这样工作:
<SolidColorBrush x:Key="Color1" Color="{Binding Source={x:Static n1:MyColors.COLOR_1}}" />
这是您的备选方案吗?名为
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命名空间:MyNamespace;assembly=MyAssembly”
...
请注意,
c
名称空间可能会被命名,例如myColor
,以便更好地识别它。标签中的符号将是myColor:myColor.MyBlue
名为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命名空间:MyNamespace;assembly=MyAssembly”
...
请注意,
c
名称空间可能会被命名,例如myColor
,以便更好地识别它。标签中的符号将是myColor:myColor.MyBlue
为什么需要在类上定义颜色。我会在xaml中定义它…因为颜色常量是在一个通用的.NETStandard 2.0程序集中定义的,并且也被WPF应用程序引用,而不仅仅是Xamarin.Forms。您能同时定义这两个吗?值作为字符串,值作为颜色?类似这样的事情:我认为不可能按照你想要的方式去做。以前有人问过。哇,我还没有在论坛上找到这个讨论。那么,替代方案是什么呢?我可以在代码中定义实际颜色,然后在XAML中静态引用它吗?为什么需要在类中定义颜色。我会在xaml中定义它…因为颜色常量是在一个通用的.NETStandard 2.0程序集中定义的,并且也被WPF应用程序引用,而不仅仅是Xamarin.Forms。您能同时定义这两个吗?值作为字符串,值作为颜色?类似这样的事情:我认为不可能按照你想要的方式去做。以前有人问过。哇,我还没有在论坛上找到这个讨论。那么,替代方案是什么呢?我可以在代码中定义实际颜色,然后在XAML中静态引用它吗?好吧,为什么不呢。尽管如此,类型转换器可能是一个很好的方式让它完成你能告诉我如何使用转换器吗?如果你愿意,我会接受你的回答,因为我肯定更喜欢XAML解决方案。尽管如此,类型转换器可能是一个很好的方式让它完成你能告诉我如何使用转换器吗?如果你这样做,我会接受你的回答,因为我肯定更喜欢XAML解决方案。现在我如何使用它来定义XAML中的颜色?仅仅这样做没有任何作用。好吧,它应该可以自动与原始代码一起工作。因此,我需要亲自尝试,并检查在这种情况下为什么不调用转换器。如果我让它工作起来,我会给你回电话的。我现在很好奇;-)它需要字符串,而不是颜色。当您使用此字符串字段且颜色为预期颜色时,将使用转换器。但正如我所说,在本例中,需要一个字符串。现在如何使用它来定义XAML中的颜色?仅仅这样做没有任何作用。好吧,它应该可以自动与原始代码一起工作。因此,我需要亲自尝试,并检查在这种情况下为什么不调用转换器。如果我让它工作起来,我会给你回电话的。我现在很好奇;-)它需要字符串,而不是颜色。当您使用此字符串字段且颜色为预期颜色时,将使用转换器。但正如我所说,在本例中,需要一个字符串。这是我在WPF中如何做到的,但这个问题是关于Xamarin.Forms的,其中SolidColorBrush不存在。然后,我将坚持你的解决方案这是我在WPF中如何做到的,但这个问题是关于Xamarin.Forms的,其中SolidColorBrush不存在。然后,我将坚持你的解决方案这是有效的,但我更喜欢我的解决方案,我可以简单地使用颜色作为StaticResource
,而不必导入名称空间等。这是可行的,但我更喜欢我的解决方案,我可以简单地使用颜色作为StaticResource
,而不必导入名称空间等。。