5

I have a user control where I have bounded a string to the xaml path. This makes it possible for me to choose colors like "Black" "Blue" and also use hexa numbers as a string to choose the color.

But I am not able to use the same string in the C# code. Which is shown in the example below:

SolidColorBrush blackBrush = new SolidColorBrush();
SolidColorBrush mySolidColorBrush = new SolidColorBrush();
mySolidColorBrush.Color = shieldGearModelRec.Gear.Color;

So the last string shieldGearModelRec.Gear.Color is what I use as a binding in XAML. And it can convert strings to color either in color names or hexa description. But how can I do this in the code behind, that is in c#?

My searches found stuff like Convert string to Color in C# but this was not possible in windows phone. Is there anyway to accomplish this?

An Idea

Do I need to create a converter that reads the string, looks for # to determine if it is hexa or a color name and then using a hexa converter to find rgb, and a switch for the names? This does not seem like the smartest solution

Community
  • 1
  • 1
JTIM
  • 2,774
  • 1
  • 34
  • 74

1 Answers1

3

One clever way I saw on net to accomplish this is by creating a string that represent XAML markup for <Color> then use XamlReader to convert the XAML string into actual Color object :

private static bool StringToColor(string strColor, out Color color)
{
    string xaml = string.Format("<Color xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\" xmlns:x=\"http://schemas.microsoft.com/winfx/2006/xaml\">{0}</Color>", strColor);
    try
    {
        object obj = XamlReader.Load(xaml);
        if (obj != null && obj is Color)
        {
            color = (Color)obj;
            return true;
        }
    }
    catch (Exception)
    {
        //Swallow useless exception
    }
    color = new Color();
    return false;
}

Example of usage :

Color newColor = new Color(); 
StringToColor(shieldGearModelRec.Gear.Color,out newColor); 
mySolidColorBrush.Color = newColor;

Note: Source of StringToColor() method can be found in George's comment to this blog post : Jim McCurdy's Tech Blog - ColorFromString for Silverlight or .NET

har07
  • 88,338
  • 12
  • 84
  • 137
  • I am testing the code now, and just to clearify tour example of usage is wrong. This is because of the out command in the function. So the function is actual quite smart that you can put it in an if case along with getting an output, smarter than I have used before :) SO the example code should be `Color newColor = new Color();` `StringToColor(shieldGearModelRec.Gear.Color,out newColor);` `mySolidColorBrush.Color = newColor;` and then one could have used an if case to ensure the test. I will mark your answer correct, after I have tested it now :) – JTIM Sep 14 '14 at 09:19
  • So unfortunately I got an error, that refers to the Color in xaml Format string. The error was: A first chance exception of type 'System.Windows.Markup.XamlParseException' occurred in System.Windows.ni.dll Additional information: The type 'Color' was not found because 'schemas.microsoft.com/winfx/2006/xaml/presentation' is an unknown namespace. [Line: 1 Position: 75] Do you have any idea of which xaml parse you could use for windows phone`? – JTIM Sep 14 '14 at 09:23
  • The error was that xmlns= needs http:// such that xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\" then the error is removed – JTIM Sep 14 '14 at 09:38
  • 1
    Sorry about those errors, I was too excited about the trick that I didn't even see the method signature correctly :) – har07 Sep 14 '14 at 10:15