標記答案是正確的答案我只是認爲我會分享我的使用情況,這與標記稍有不同,但僅限於我的具體需求。
在我來說,我想要的是一套新的顏色,以便能夠通過名稱進行選擇,以便實現這個我使用的字典
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
很高興爲您服務。我忘記提及一件非常重要的事情:如果您打算使用自定義擴展而不必聲明名稱空間,那麼您需要將它放在它自己的類庫項目中。不知道爲什麼,但其他人也遇到了它,似乎是在框架中的錯誤。 –