-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Description
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(); |
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
- Create a ResourceDictionary with OnPlatform resource of type Color.
- Set the Background property of MainPage to that Color as a DynamicResource.
- Merge the dictinary to the resources of the MainPage.
- 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
Assignees
Labels
Type
Projects
Status