Bug 37675 - Style PropertyChanged called before the Style is applied
Summary: Style PropertyChanged called before the Style is applied
Status: RESOLVED FEATURE
Alias: None
Product: Forms
Classification: Xamarin
Component: Forms ()
Version: 2.0.0
Hardware: All All
: --- normal
Target Milestone: ---
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2016-01-14 03:47 UTC by Brad Zacher
Modified: 2016-01-22 10:34 UTC (History)
5 users (show)

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

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 FEATURE

Description Brad Zacher 2016-01-14 03:47:21 UTC
Problem:
For all VisualElement objects, the PropertyChanged event is called *BEFORE* the Style is applied to the object.
This means that any attempts to get properties within the PropertyChanged method will not work.

Reproduction:

var label = new Label();
label.PropertyChanged += (sender, args) =>
{
    if (args.PropertyName == "Style" || args.PropertyName == "TextColor")
    {
        Console.WriteLine(label.TextColor == Color.Red);
    }
};
label.TextColor = Color.Red;
label.Style = new Style(typeof(Label))
              {
                  Setters =
                  {
                      new Setter
                      {
                          Property = Label.TextColorProperty,
                          Value = Color.Blue
                      }
                  }
              };

Will print:
True
True

Instead of:
True
False


Cause:
The StyleProperty is constructed with a BindableProperty.PropertyChanged delegate (which refers to the VisualElement.OnStylePropertyChanged method).
However, within the BindableObject.SetValueActual method, BindableProperty.PropertChanged is called *AFTER* BindableObject.OnPropertyChanged.
Comment 1 Rui Marinho 2016-01-20 11:47:58 UTC
Should be fixed in 2.1.0-pre1
Comment 2 Stephane Delcroix 2016-01-22 10:34:54 UTC
The fix won't be in the next release as it was breaking multiple other stuffs.

As always, with events with multiple handlers, the order matters, and the order that's fine for one is probably not for others.

When you receive the PropertyChanged event, you can be sure that the property has changed. There's is no guarantee that the property is fully applied. It's a bit like when you set the Text of a Label, the PropertyChanged event is fired as soon as the property has changed, but that doesn't mean the Layout has adapted yet to the new size of the Label.

We might consider changing that in the future, but it's too much of a breaking change for all of our users right now.

Thanks for understanding this.

the xf team