标记答案是正确的答案我只是认为我会分享我的使用情况,这与标记稍有不同,但仅限于我的具体需求。
在我来说,我想要的是一套新的颜色,以便能够通过名称进行选择,以便实现这个我使用的字典
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
}
命名空间后调整马克建议我的用法是:
{PrimaryPalette CustomMagenta}
此方法还为您提供智能感知和编译时验证以确保颜色名称正确。
这太棒了,正是我在找的东西。感谢您的帮助 – Colin
很高兴为您服务。我忘记提及一件非常重要的事情:如果您打算使用自定义扩展而不必声明名称空间,那么您需要将它放在它自己的类库项目中。不知道为什么,但其他人也遇到了它,似乎是在框架中的错误。 –