[英]Xaml reference static properties like System.Windows.Media.Colors
I'm defining a custom palette class in a similar way to the inbuilt System.Windows.Media.Colors static class. 我正在以类似于内置System.Windows.Media.Colors静态类的方式定义自定义调色板类。
Ideally I would like to reference this in a similar way to the Colors class. 理想情况下,我想以与Colors类类似的方式引用它。 eg 例如
<Rectangle Fill="Red"/>
The most concise version I have so far is: 到目前为止,我最简洁的版本是:
<Rectangle Fill="{x:Static Palette:PrimaryPalette.Red}"/>
Also my PrimaryPalette class has properties with a return type SolidColorBrush rather than Color. 我的PrimaryPalette类还具有返回类型为SolidColorBrush而不是Color的属性。
So my question is two fold 所以我的问题有两个
If verbosity and ease-of-use is your primary concern then I would recommend using a custom MarkupExtension: 如果您最关心冗长和易于使用,那么我建议您使用自定义MarkupExtension:
public class CustomColor : MarkupExtension
{
[ConstructorArgument("Key")]
public object Key { get; set; }
public CustomColor()
{
}
public CustomColor(object key)
{
this.Key = key;
}
public override object ProvideValue(IServiceProvider serviceProvider)
{
try
{
var color = (Color)ColorConverter.ConvertFromString(this.Key.ToString());
return new SolidColorBrush(color);
}
catch
{
return new SolidColorBrush(Colors.Transparent);
}
}
}
By following the tips on this page you can also add the namespace for this extension to the XAML processor which in turn allows you to use it like this: 通过遵循此页面上的提示,您还可以将此扩展名的名称空间添加到XAML处理器,从而允许您像这样使用它:
<Rectangle Fill="{CustomColor Blue}" />
This is just one way of doing things, in this particular example I'm simply parsing the key string and converting it to a color, but you can replace that with any key type and/or conversion code you want. 这只是一种处理方式,在这个特定示例中,我只是解析键字符串并将其转换为颜色,但是您可以将其替换为所需的任何键类型和/或转换代码。
Marks answer is the correct answer I just thought I would share my usage which is slightly different than Marks but only due to my specific needs. Marks的答案是正确的答案,我只是想与他人分享我的用法,这与Marks略有不同,但这仅是出于我的特定需求。
In my case I wanted a set of new colours to be able to be selected by name so to achieve this I utilised a dictionary 就我而言,我希望能够通过名称选择一组新颜色,因此为了实现这一目标,我使用了词典
public class PrimaryPalette : MarkupExtension
{
public static Dictionary<PrimaryPaletteColours, Color> Palette =>
new Dictionary<PrimaryPaletteColours, Color>
{
{ PrimaryPaletteColours.CustomMagenta, Color.FromArgb(0xFF, 0xDA, 0x42, 0xAA) },
{ PrimaryPaletteColours.CustomBlue, Color.FromArgb(0xFF, 0x11, 0x42, 0xFF) },
{ PrimaryPaletteColours.CustomGreen, Color.FromArgb(0xFF, 0x33, 0xDE, 0x60) },
{ PrimaryPaletteColours.CustomOrange, Color.FromArgb(0xFF, 0xDA, 0x80, 0x22) },
{ PrimaryPaletteColours.CustomPurple, Color.FromArgb(0xFF, 0xCC, 0x00, 0xFF) },
{ PrimaryPaletteColours.CustomRed, Color.FromArgb(0xFF, 0xEE, 0x42, 0x00) },
{ PrimaryPaletteColours.CustomTurqoise, Color.FromArgb(0xFF, 0x10, 0xAB, 0xBC) },
{ PrimaryPaletteColours.CustomGold, Color.FromArgb(0xFF, 0xDA, 0xA0, 0x22) }
};
public PrimaryPalette() { }
public PrimaryPalette(PrimaryPaletteColours key) { Key = key; }
[ConstructorArgument("Key")]
public PrimaryPaletteColours Key { get; set; }
public override object ProvideValue(IServiceProvider serviceProvider)
{
try
{
return new SolidColorBrush(Palette[Key]);
}
catch
{
return new SolidColorBrush(Colors.Transparent);
}
}
}
public enum PrimaryPaletteColours
{
CustomMagenta,
CustomBlue,
CustomGreen,
CustomOrange,
CustomPurple,
CustomRed,
CustomTurqoise,
CustomGold
}
After the namespace tweak Mark suggested my usage is: 在名称空间调整后,Mark建议我的用法是:
{PrimaryPalette CustomMagenta}
This method also gives you intellisense and compile time verification to ensure the color name is correct. 此方法还为您提供了智能感知和编译时间验证,以确保颜色名称正确。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.