Bug 47157 - BindableProperty for Color in RoutingEffect returning empty Color
Summary: BindableProperty for Color in RoutingEffect returning empty Color
Status: RESOLVED ANSWERED
Alias: None
Product: Forms
Classification: Xamarin
Component: Forms ()
Version: 2.3.3
Hardware: PC Windows
: --- normal
Target Milestone: ---
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2016-11-17 01:31 UTC by Tomasz Cielecki
Modified: 2017-03-20 03:23 UTC (History)
2 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:


Attachments
Sample solution (467.41 KB, application/x-zip-compressed)
2016-11-17 01:31 UTC, Tomasz Cielecki
Details


Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED ANSWERED

Description Tomasz Cielecki 2016-11-17 01:31:03 UTC
Created attachment 18493 [details]
Sample solution

I was following the Xamarin University 330 Effects class and added a couple of more sliders to control the gradient of a button for R, G and B and not all channels at the same time as described in the course material.

A RoutingEffect is defined in this class, which has a BindableProperty, which is a color. This color is routed to PlatformEffect's, which seems to work OK.

There are two methods SetGradientColor and GetGradientColor, which are used to call SetValue and GetValue on the BindableProperty.

When changing a slider to change the Color in the BindableProperty, what I do first is to call GetGradientColor to get the current Color. Then I grab the R, G and B values and replacing the value I am currently adjusting with the slider value. Then I call SetGradientColor to set it on the view. This looks something like:

    var oldColor = ButtonGradientEffect.GetGradientColor(buttonClick);
    Color gradColor = new Color(e.NewValue / 255.0, oldColor.G / 255.0, oldColor.B / 255.0);
    ButtonGradientEffect.SetGradientColor(buttonClick, gradColor);

e.NewValue just swaps places depending on what color I am adjusting.

Now, here's the issue. Adjusting one of the sliders, then adjusting another slider, makes the value from the first slider suddenly reset to 0.

I tried this on both UWP and Android and this seems to be the case on both platforms.

I have attached the solution where this happens. The the old and new color will be written to the output window, but it is easy to spot on the button when adjusting the sliders.

The code in question is resided in the PCL in the MainPage.xaml.cs
Comment 1 Paul DiPietro [MSFT] 2017-03-20 03:23:36 UTC
In your sliders' value changed methods you're doing calculations like this:

Color gradColor = new Color(e.NewValue / 255.0, oldColor.G / 255.0, oldColor.B / 255.0);

Those values as you adjust the slider are on a 0-1 decimal scale, which makes sense as far as dividing by 255. However, if you do something such as adjust the G value after R has been adjusted, you might have oldColor.R be equal to .15, which when divided by 255 is what's causing your 0 value when you have no reason to adjust it because it's the same value as before. Change your methods to only divide by 255 on the newValue and use the oldColor values as they are and you'll see that the gradient is changing color correctly.