locked
ColorToBrushConverter in Xaml Resources RRS feed

  • Question

  • Hello,

    Trying to implement this in my xaml code:

     <Page.Resources>
            <converters:ColorToBrushConverter x:Key="clr-namespace:CustomColorsPicker.Converters" />
        </Page.Resources>

    These are the errors I am getting:

    Error	1	Unknown type 'ColorToBrushConverter' in XML namespace 'clr-namespace:CustomColorsPicker.Converters;assembly=ScreenText, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'	18	10	ScreenText
    Error	2	The name "ColorToBrushConverter" does not exist in the namespace "clr-namespace:CustomColorsPicker.Converters"	18	9	ScreenText
    

    No idea how to fix it. 

    Any help would be nice!

    -Vincent

    Wednesday, October 22, 2014 6:13 PM

Answers

  • I think the problem is that you're mixing Windows Runtime and Silverlight syntax here.

    You probably want something like:

    <Page
        ...
        xmlns:converters="using:ScreenText.Converters"
        ...>
    
    <Page.Resources>
        <converters:ColorToBrushConverter x:Key=ColorToBrush />
    </Page.Resources>
    
    <Grid>
       ...
       <Rectangle Fill="{Binding MyColor, Converter={StaticResource ColorToBrush}" />
       ...
    </Grid>
    

    If you need more help with this can you please share a minimal sample demonstrating what you're doing to your OneDrive?

    --Rob

    Thursday, October 23, 2014 8:20 PM

All replies

  • How are you implementing your ColorToBrushConverter?

    This isn't a built-in class. It is something you would need to include in your code.

    Wednesday, October 22, 2014 7:11 PM
  • Hello Rob,

    I made a separate class indeed:

    using System;
    using System.Threading;
    using Windows.UI.Xaml.Data;
    using Windows.UI.Xaml.Media;
    using Windows.UI;
    
    namespace ScreenText.Converters
    {
        public class ColorToBrushConverter : IValueConverter
        {
            public object Convert(object value, Type targetType, object parameter, string language)
            {
                if (value != null)
                {
                    return new SolidColorBrush((Color)(value));
                }
             return null;
            }
    
            public object ConvertBack(object value, Type targetType, object parameter, string language)
            {
                throw new NotImplementedException();
            }
        }
    }

    -Vincent

    Wednesday, October 22, 2014 7:20 PM
  • In your Xaml you say it is in CustomColorsPicker.Converters namespace.

    In your code it's in the ScreenText.Converters namespace.

    Wednesday, October 22, 2014 7:49 PM
  • Yeah I fixed that now, but error 1 is still there, with switched names ofcourse

    Error	3	Unknown type 'ColorToBrushConverter' in XML namespace 'clr-namespace:ScreenText.Converters;assembly=ScreenText, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'

    Wednesday, October 22, 2014 8:00 PM
  • You need to show more of your xaml. How did you define the converters xmlns?
    Wednesday, October 22, 2014 9:31 PM
  • Create a new class as follows:

    public static class ColorToBrush
    {
        public static Brush ConvertToBrush(Color color)
        {
             return new SolidColorBrush(color);
        }
    }

    You may need to add a few more using statements for the Brush, Color, and SolidColorBrush classes to be available.  If you don't want the class, you can paste the method and it will work the same.

    Hope this helps!!


    ~ TechPerson32


    Thursday, October 23, 2014 1:44 AM
  • Hi mSpot Inc

    xmlns:converters="clr-namespace:ScreenText.Converters"

    In my page section.


    • Edited by Vincent Gio Thursday, October 23, 2014 7:03 AM Target
    Thursday, October 23, 2014 6:22 AM
  • Hi TechPerson32_, so this would be a new class file?
    • Edited by Vincent Gio Thursday, October 23, 2014 7:03 AM Target
    Thursday, October 23, 2014 6:22 AM
  • TechPerson's code isn't a ValueConverter. It won't replace what I think you're trying to do.
    Thursday, October 23, 2014 7:37 PM
  • Your xaml is wrong. Try this:

    <Page.Resources>
        <converters:ColorToBrushConverter x:Key="ColorToBrushConverter" />
    </Page.Resources>


    and you will be using it like this:

        Converter={StaticResource ColorToBrushConverter}

    • Edited by mSpot Inc Thursday, October 23, 2014 8:18 PM
    Thursday, October 23, 2014 8:16 PM
  • I think the problem is that you're mixing Windows Runtime and Silverlight syntax here.

    You probably want something like:

    <Page
        ...
        xmlns:converters="using:ScreenText.Converters"
        ...>
    
    <Page.Resources>
        <converters:ColorToBrushConverter x:Key=ColorToBrush />
    </Page.Resources>
    
    <Grid>
       ...
       <Rectangle Fill="{Binding MyColor, Converter={StaticResource ColorToBrush}" />
       ...
    </Grid>
    

    If you need more help with this can you please share a minimal sample demonstrating what you're doing to your OneDrive?

    --Rob

    Thursday, October 23, 2014 8:20 PM
  • Hi Rob,

    That fixed some issues but I am still getting errors.

    Seeing that you are a legitimate Moderator I would be happy to just send you the app since there's just small stuff here and there, nothing secret :)

    I can share it over OneDrive, so just send me your E-Mail!

    -Vincent

    Tuesday, November 4, 2014 10:55 AM
  • If you'd like to share a minimal sample on your OneDrive then I (and other community members) can take a look.

    Tuesday, November 4, 2014 6:46 PM
  • Hi Rob,

    Basically it is just a copy of this, followed it step by step. 

    If you want you can check it out and see if you can pinpoint edits, or I can assemble it and Share it.

    -Vincent

    Tuesday, November 4, 2014 7:45 PM
  • Hi Vincent,

    The blog you linked is designed for a Windows Phone Silverlight app, not a Windows Phone Runtime app. The basic concepts will be the same, but some of the details will be different.

    I can't port somebody else's code for you, but if you have specific, targeted questions then I (or others) may be able to help with those. As you noted previously, it'd be better to post different questions in different threads.

    --Rob

    Tuesday, November 4, 2014 9:34 PM