How to set color for a BoxView ?

XavierBXavierB USMember
edited June 2016 in Xamarin.Forms

Hi there !

I'm trying to set a dynamic color for my BoxView, but the refresh taking a long time... And the result doesn't show all the colors I want.

I would like to create a animation who change color of RGB (red -> -> blue -> ... -> yellow -> red (repeat)) smoothly.

The algorithm work good, but the bv_filter.Color = Color.FromRgb(r, g, b); doesn't want to refresh all times.

Here is my code :
`public StartPage()
{
InitializeComponent();

        this.Appearing += async (object sender, EventArgs e) =>
        {
            while (true)
            {
                await change_color(10);    
            }
        };
    }

    private async Task<bool> change_color(Int32 delay)
    {
        double r = 255;
        double g = 0;
        double b = 0;

        while (r != 0 | g != 0 | b != 0)
        {
            if (r == 255 & g == 0)
                b ++;

            if (b == 255 & g == 0)
                r --;

            if (b == 255 & r == 0)
                g ++;

            if (g == 255 & r == 0)
                b --;

            if (g == 255 & b == 0)
                r ++;

            if (r == 255 & b == 0)
                g --;

            // Debug.WriteLine(r + "," + g + "," + b);
            bv_filter.Color = Color.FromRgb(r, g, b);

            await Task.Delay(delay);

        }

        return true;
    }`

I'm looking for the PropertyChanged, but I doesn't find answer... :/

I'm newbie in Xamarin, thanks in advance for anyone who can give me some help :)

Sorry for my bad english...

Tagged:

Best Answer

Answers

  • Chasy_AChasy_A Member ✭✭

    Hi,

    According to this:
    docs.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/colors
    The values for Color.FromRgb(r, g, b);are [0-255] but it does not work, as you wrote :(
    Can you please add here your solution with the values [0-1]?

    Thanks!

  • JohnHardmanJohnHardman GBUniversity mod
    edited July 2018

    There are two forms of Color.FromRgb, as follows. If you are having problems with the second of these, check that the arguments you specify are doubles (or floats, which would get converted to doubles) in the range 0.0 - 1.0

        public static Color FromRgb(int r, int g, int b);
        public static Color FromRgb(double r, double g, double b);
    
Sign In or Register to comment.