![]() PublicKeyToken=31bf3856ad364e35").GetProperties() ĭim clrs As PropertyInfo() = Type.GetType( _ NOTE: This code is not used in this project, it is just here as an example of how to use reflection to get the list of properties of a Color object. Below is some sample code you might write to retrieve and print out each individual color name from the Colors class. Once you have the Name property you can bind that property to the list box shown in Figure 1. So once you have each Color object, you then need all of its properties so you can retrieve the Name property. Each property of the Colors class is a Color object. So you need to use reflection to get the collection of properties. To retrieve the WPF Colors is also a little tricky, as the Colors class contains individual static/Shared properties, one property to represent each color. Once I have that, I usually find it is very easy to convert that code to use an ObjectDataProvider. One thing that helps me immensely – and I hope will work for you - is to write a little code that will return a collection of items. You can get the complete list of Colors using an ObjectDataProvider object, however setting up the ObjectDataProvider can be a little tricky. The first thing this utility needs to do is to load all the WPF colors into a list box. In Figure 1 you can see a utility I wrote to help me do this.įigure 1: Utility to display all the WPF colors and retrieve the RGB values Thus, I needed a little utility that would help me get these numbers for the color I was looking for. Well, I don’t know about you, but I do not remember all of the (R)ed, (G)reen, and (B)lue color numbers off the top of my head. Thus, I need to create a Color resource that looks like the following: I can’t just create a string resource and put that into the Color attribute. Instead of hard-coding the color “Gray”, I would like to use a resource. This element has a “Color” attribute that you can set as shown in the code below: For example, I am using the on many of my controls. Instead of hard-coding colors, I use resources. During this project, I needed to set a lot of colors. Now you have got the color for the hex you have provided.Recently, I have been setting up a lot of WPF styles for an application I am working on. Now all that left is to get the color by Calling the method and pass the hex string to it as parameter: var color = GetSolidColorBrush("#FFCD3927").Color SolidColorBrush myBrush = new SolidColorBrush(Windows.UI.Color.FromArgb(a, r, g, b)) If the above explanation got you confused and you just want to convert the hex to Color and don't want to be bothered or to get deep into it just use following method :Ĭreate a method to Convert Hex string to SolidColorBrush: public SolidColorBrush GetSolidColorBrush(string hex)īyte a = (byte)(Convert.ToUInt32(hex.Substring(0, 2), 16)) īyte r = (byte)(Convert.ToUInt32(hex.Substring(2, 2), 16)) īyte g = (byte)(Convert.ToUInt32(hex.Substring(4, 2), 16)) īyte b = (byte)(Convert.ToUInt32(hex.Substring(6, 2), 16)) In otherwords, the global behaviors, UI and interaction can be reused using the classes available in all frameworks, usually represented by a Portable Class Library (PCL), and each version of your app ( Winforms, Universal, Xamarin, etc) would reuse that shared library for the "core" of the application and the UI (and platform-specific behavior) would be the only part you would implement separately, in each version of the app you want to support. instead, what you want to do is abstract the platform-specific behavior to an interface, and only that part has to be rewritten. However, you don't necessarily have to rewrite the WHOLE THING. net (such as System.Drawing) in a Universal Project. NET framework, and as a result, you can't reuse everything from full. Windows Universal apps don't have the same coverage of namespaces, classes, and APIs as the full. The issue here isn't the framework version but rather the reduced framework API that is available to Universal Apps. You may hit dependency error like 'System.drawing namespace not found' or 'ColorConverter does not exist'. net Applications the code above will work, but not in UWP Apps or previous Windows Apps. Something from the following thread from StackOverflow string hex = "#FFFFFF" Ĭolor color = (hex) The most common solution you find will be some thing like the code below which can be found in the following thread in StackOverflow using Ĭolor color = (Color)ColorConverter.ConvertFromString("#FFDFD991") You must have googled to find the solution, or performed a search in Stack-overflow If you have developed apps for Universal Windows Platform there must be situations where you had to convert Hex-code to Color. Menu Converting Hex to Color in C# for Universal Windows Platform (UWP) 08 March 2016 on C#, UWP, Universal Windows App, Windows 10
0 Comments
Leave a Reply. |