如何通过WinRT中的颜色进行枚举?

在非WinRT中枚举颜色是一个常见问题,答案很简单。 但是,由于Colors’ENUM’实际上只是一个具有静态“颜色”属性的类,因此您无法在WinRT中使用标准方法。

你如何枚举WinRT中的颜色?

像这样:

在WinRT中枚举颜色需要using System.Reflection以便您可以获取容器类“颜色”中子类的静态属性。 像这样:

 Dictionary Colors() { var _Colors = typeof(Windows.UI.Colors) // using System.Reflection; .GetRuntimeProperties() .Select(c => new { Color = (Windows.UI.Color)c.GetValue(null), Name = c.Name }); return _Colors.ToDictionary(x => x.Name, x => x.Color); } 

资料来源: http : //codepaste.net/j3mzrw

注意:如果你不喜欢reflection(出于某种原因),那么也没有什么可以阻止你手工编码可枚举的颜色。 创建一个“动态”颜色列表实际上只是花哨的缘故 – 颜色不会更新。 只需写清单!

在这里,我为你做了:

 public class ColorList : List { public ColorList() { this.Add(Windows.UI.Colors.AliceBlue); this.Add(Windows.UI.Colors.AntiqueWhite); this.Add(Windows.UI.Colors.Aqua); this.Add(Windows.UI.Colors.Aquamarine); this.Add(Windows.UI.Colors.Azure); this.Add(Windows.UI.Colors.Beige); this.Add(Windows.UI.Colors.Bisque); this.Add(Windows.UI.Colors.Black); this.Add(Windows.UI.Colors.BlanchedAlmond); this.Add(Windows.UI.Colors.Blue); this.Add(Windows.UI.Colors.BlueViolet); this.Add(Windows.UI.Colors.Brown); this.Add(Windows.UI.Colors.BurlyWood); this.Add(Windows.UI.Colors.CadetBlue); this.Add(Windows.UI.Colors.Chartreuse); this.Add(Windows.UI.Colors.Chocolate); this.Add(Windows.UI.Colors.Coral); this.Add(Windows.UI.Colors.CornflowerBlue); this.Add(Windows.UI.Colors.Cornsilk); this.Add(Windows.UI.Colors.Crimson); this.Add(Windows.UI.Colors.Cyan); this.Add(Windows.UI.Colors.DarkBlue); this.Add(Windows.UI.Colors.DarkCyan); this.Add(Windows.UI.Colors.DarkGoldenrod); this.Add(Windows.UI.Colors.DarkGray); this.Add(Windows.UI.Colors.DarkGreen); this.Add(Windows.UI.Colors.DarkKhaki); this.Add(Windows.UI.Colors.DarkMagenta); this.Add(Windows.UI.Colors.DarkOliveGreen); this.Add(Windows.UI.Colors.DarkOrange); this.Add(Windows.UI.Colors.DarkOrchid); this.Add(Windows.UI.Colors.DarkRed); this.Add(Windows.UI.Colors.DarkSalmon); this.Add(Windows.UI.Colors.DarkSeaGreen); this.Add(Windows.UI.Colors.DarkSlateBlue); this.Add(Windows.UI.Colors.DarkSlateGray); this.Add(Windows.UI.Colors.DarkTurquoise); this.Add(Windows.UI.Colors.DarkViolet); this.Add(Windows.UI.Colors.DeepPink); this.Add(Windows.UI.Colors.DeepSkyBlue); this.Add(Windows.UI.Colors.DimGray); this.Add(Windows.UI.Colors.DodgerBlue); this.Add(Windows.UI.Colors.Firebrick); this.Add(Windows.UI.Colors.FloralWhite); this.Add(Windows.UI.Colors.ForestGreen); this.Add(Windows.UI.Colors.Fuchsia); this.Add(Windows.UI.Colors.Gainsboro); this.Add(Windows.UI.Colors.GhostWhite); this.Add(Windows.UI.Colors.Gold); this.Add(Windows.UI.Colors.Goldenrod); this.Add(Windows.UI.Colors.Gray); this.Add(Windows.UI.Colors.Green); this.Add(Windows.UI.Colors.GreenYellow); this.Add(Windows.UI.Colors.Honeydew); this.Add(Windows.UI.Colors.HotPink); this.Add(Windows.UI.Colors.IndianRed); this.Add(Windows.UI.Colors.Indigo); this.Add(Windows.UI.Colors.Ivory); this.Add(Windows.UI.Colors.Khaki); this.Add(Windows.UI.Colors.Lavender); this.Add(Windows.UI.Colors.LavenderBlush); this.Add(Windows.UI.Colors.LawnGreen); this.Add(Windows.UI.Colors.LemonChiffon); this.Add(Windows.UI.Colors.LightBlue); this.Add(Windows.UI.Colors.LightCoral); this.Add(Windows.UI.Colors.LightCyan); this.Add(Windows.UI.Colors.LightGoldenrodYellow); this.Add(Windows.UI.Colors.LightGray); this.Add(Windows.UI.Colors.LightGreen); this.Add(Windows.UI.Colors.LightPink); this.Add(Windows.UI.Colors.LightSalmon); this.Add(Windows.UI.Colors.LightSeaGreen); this.Add(Windows.UI.Colors.LightSkyBlue); this.Add(Windows.UI.Colors.LightSlateGray); this.Add(Windows.UI.Colors.LightSteelBlue); this.Add(Windows.UI.Colors.LightYellow); this.Add(Windows.UI.Colors.Lime); this.Add(Windows.UI.Colors.LimeGreen); this.Add(Windows.UI.Colors.Linen); this.Add(Windows.UI.Colors.Magenta); this.Add(Windows.UI.Colors.Maroon); this.Add(Windows.UI.Colors.MediumAquamarine); this.Add(Windows.UI.Colors.MediumBlue); this.Add(Windows.UI.Colors.MediumOrchid); this.Add(Windows.UI.Colors.MediumPurple); this.Add(Windows.UI.Colors.MediumSeaGreen); this.Add(Windows.UI.Colors.MediumSlateBlue); this.Add(Windows.UI.Colors.MediumSpringGreen); this.Add(Windows.UI.Colors.MediumTurquoise); this.Add(Windows.UI.Colors.MediumVioletRed); this.Add(Windows.UI.Colors.MidnightBlue); this.Add(Windows.UI.Colors.MintCream); this.Add(Windows.UI.Colors.MistyRose); this.Add(Windows.UI.Colors.Moccasin); this.Add(Windows.UI.Colors.NavajoWhite); this.Add(Windows.UI.Colors.Navy); this.Add(Windows.UI.Colors.OldLace); this.Add(Windows.UI.Colors.Olive); this.Add(Windows.UI.Colors.OliveDrab); this.Add(Windows.UI.Colors.Orange); this.Add(Windows.UI.Colors.OrangeRed); this.Add(Windows.UI.Colors.Orchid); this.Add(Windows.UI.Colors.PaleGoldenrod); this.Add(Windows.UI.Colors.PaleGreen); this.Add(Windows.UI.Colors.PaleTurquoise); this.Add(Windows.UI.Colors.PaleVioletRed); this.Add(Windows.UI.Colors.PapayaWhip); this.Add(Windows.UI.Colors.PeachPuff); this.Add(Windows.UI.Colors.Peru); this.Add(Windows.UI.Colors.Pink); this.Add(Windows.UI.Colors.Plum); this.Add(Windows.UI.Colors.PowderBlue); this.Add(Windows.UI.Colors.Purple); this.Add(Windows.UI.Colors.Red); this.Add(Windows.UI.Colors.RosyBrown); this.Add(Windows.UI.Colors.RoyalBlue); this.Add(Windows.UI.Colors.SaddleBrown); this.Add(Windows.UI.Colors.Salmon); this.Add(Windows.UI.Colors.SandyBrown); this.Add(Windows.UI.Colors.SeaGreen); this.Add(Windows.UI.Colors.SeaShell); this.Add(Windows.UI.Colors.Sienna); this.Add(Windows.UI.Colors.Silver); this.Add(Windows.UI.Colors.SkyBlue); this.Add(Windows.UI.Colors.SlateBlue); this.Add(Windows.UI.Colors.SlateGray); this.Add(Windows.UI.Colors.Snow); this.Add(Windows.UI.Colors.SpringGreen); this.Add(Windows.UI.Colors.SteelBlue); this.Add(Windows.UI.Colors.Tan); this.Add(Windows.UI.Colors.Teal); this.Add(Windows.UI.Colors.Thistle); this.Add(Windows.UI.Colors.Tomato); this.Add(Windows.UI.Colors.Transparent); this.Add(Windows.UI.Colors.Turquoise); this.Add(Windows.UI.Colors.Violet); this.Add(Windows.UI.Colors.Wheat); this.Add(Windows.UI.Colors.White); this.Add(Windows.UI.Colors.WhiteSmoke); this.Add(Windows.UI.Colors.Yellow); this.Add(Windows.UI.Colors.YellowGreen); } } 

要么有效。