Search code examples
c#xamarinrenderer

Xamaring Windows 8.1 Custom Rendered convert hex to Brush?


I am trying to follow Custom rendered process as defined here:

https://developer.xamarin.com/guides/xamarin-forms/custom-renderer/entry/

I am trying to do the windows one, I am just running the windows one not the windows phone. But I cant figure out how to convert a hex colour into the required windows SolidColorBrush

How do i do this. I getting rather confused with the different dlls as Color exists in the portable class library too, but is not compatible.

If someone could test this and show me a working copy that would really be appreciated. Am I missing a reference?

using Windows.UI;
using Windows.UI.Xaml.Media;
using Hello.Renderer;
using Hello.Windows.Renderer;
using Xamarin.Forms;
using Xamarin.Forms.Platform.WinRT;

[assembly:ExportRenderer(typeof(MyEntry), typeof(MyEntryRenderer))]
namespace Hello.Windows.Renderer
{
    public class MyEntryRenderer : EntryRenderer
    {
        protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
        {
            base.OnElementChanged(e);

            if (Control != null)
            {
                Control.Background = (SolidColorBrush)new ColorConverter().Convert("#FF6A00", null, null, null);
            }
        }
    }
}

Solution

  • Try with this converter:

    public static SolidColorBrush GetColorFromHexa(string hexaColor)
    {
        return new SolidColorBrush(
            Color.FromArgb(
                255,
                Convert.ToByte(hexaColor.Substring(1, 2), 16),
                Convert.ToByte(hexaColor.Substring(3, 2), 16),
                Convert.ToByte(hexaColor.Substring(5, 2), 16)
            )
        );
    }