Skip to content

Application freezes when Background property is set to a DynamicResource of type OnPlatform Color #27281

@APopatanasov

Description

@APopatanasov

Description

I try to dynamically change the Background property of my Page. Because of that the Background is set to a DynamicResource. When I try to merge the ResourceDictionary that contains the value of the Background the application freezes and is no longer responsive. This happens when the DynamicResource is of type OnPlatform:

<OnPlatform x:Key="AppSurfaceColor" x:TypeArguments="Color" Default="#000000">
    <On Platform="Android" Value="#121212" />
    <On Platform="WinUI" Value="#1C1C1C" />
    <On Platform="iOS" Value="#FF0000" />
    <On Platform="MacCatalyst" Value="#0000FF" />
</OnPlatform> 

I haven't tested it on net8, but I believe the issue will be present there as well. It is basically caused by this line of code:

(bindable as VisualElement)?.NotifyBackgroundChanges();
that method calls OnParentResourcesChanged(this.GetMergedResources());.

Because of that the same logic that is invoked when the dictionary is merged is invoked again. OnPlatform creates a new Color, a new SolidColorBrush is created as well and that brush is set to the Background property. BackgroundChanged is invoked which calls OnParentResourcesChanged and we cannot break from that loop.

What I was expecting was the second call caused by the property changed of the Background property to prevent the logic from going into infinity calls of OnParentResourcesChanged. However, as Color is a class OnPlatform creates it every time and when we compare the old Background brush with the new one they are different because Equals of the SolidColorBrush uses "==" to check the colors:

return Color == dest.Color;

When Equals of Color is used instead of "==" in the SolidColorBrush everything is working as expected. I was able to test that as a potential fix while debugging the source code looking for a workaround.

Steps to Reproduce

  1. Create a ResourceDictionary with OnPlatform resource of type Color.
  2. Set the Background property of MainPage to that Color as a DynamicResource.
  3. Merge the dictinary to the resources of the MainPage.
  4. The application freezes.

Check the attached sample project. Run the project and click on the Button. On click we are merging the ResourceDictionary that contains the resource the Background is set to.

Link to public reproduction project repository

https://github.com/telerik/ms-samples/tree/main/Maui/DynamicBackgroundIssue

Version with bug

9.0.30 SR3

Is this a regression from previous behavior?

No, this is something new

Last version that worked well

Unknown/Other

Affected platforms

iOS, Android, Windows, macOS

Affected platform versions

No response

Did you find any workaround?

Use BackgroundColor instead of Background.

Relevant log output

Metadata

Metadata

Labels

area-xamlXAML, CSS, Triggers, Behaviorsi/regressionThis issue described a confirmed regression on a currently supported versionpartnerIssue or Request from a partner teamplatform/androidplatform/iosplatform/macosmacOS / Mac Catalystplatform/windowsregressed-in-9.0.0s/triagedIssue has been revieweds/verifiedVerified / Reproducible Issue ready for Engineering Triaget/bugSomething isn't working

Type

No type

Projects

Status

In Progress

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions