Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[regression/8.0.0] DynamicResource not working with gradients when switching themes at runtime #18545

Closed
Zack-G-I-T opened this issue Nov 6, 2023 · 15 comments · Fixed by #20637
Assignees
Labels
area-xaml XAML, CSS, Triggers, Behaviors fixed-in-8.0.10 fixed-in-9.0.0-preview.2.10293 i/regression This issue described a confirmed regression on a currently supported version platform/android 🤖 s/triaged Issue has been reviewed s/verified Verified / Reproducible Issue ready for Engineering Triage t/bug Something isn't working
Milestone

Comments

@Zack-G-I-T
Copy link

Zack-G-I-T commented Nov 6, 2023

Description

When using a DynamicResource to change colors between light and dark mode in a gradient - .NET 8 no longer works. This worked fine in .NET7. The background stays on the light theme version the whole time.

<Border.Background>
                    <LinearGradientBrush EndPoint="0,1">
                        <GradientStop Offset="0.1" Color="{DynamicResource GradientColorStart}" />
                        <GradientStop Offset="1.0" Color="{DynamicResource GradientColorEnd}" />
                    </LinearGradientBrush>
</Border.Background>

Steps to Reproduce

  1. Download repo
  2. Press button and see issue - in dark mode the border should be purple and orange gradient.

Link to public reproduction project repository

https://github.com/Zack-G-I-T/DynamicResourceNet8

Version with bug

8.0.0-preview.7.8842

Is this a regression from previous behavior?

Yes, this used to work in .NET MAUI

Last version that worked well

7.0.96

Affected platforms

Android

Affected platform versions

No response

Did you find any workaround?

Using AppThemeBinding with Light and Dark, but our current app has two theme files for Light and Dark Theme so we use the same color names for both files. It will be very tedious to add "Dark" version to all the colours.

Relevant log output

No response

@Zack-G-I-T Zack-G-I-T added the t/bug Something isn't working label Nov 6, 2023
@jsuarezruiz jsuarezruiz added the area-xaml XAML, CSS, Triggers, Behaviors label Nov 7, 2023
@developer9969
Copy link

@davidortinau Can you help prioritize this issue?
we have just hit this problem too! We need the ability to swap merged resource dictionary at run time, we handle many themes that apptheme binding could not cope with. How are we supposed to swap themes now in .net 8 at run time?

We have come across quite a few xaml resource issues recently , this area used to be solid in xamarin.

@XamlTest XamlTest added s/verified Verified / Reproducible Issue ready for Engineering Triage s/triaged Issue has been reviewed labels Nov 8, 2023
@samhouts samhouts added the potential-regression This issue described a possible regression on a currently supported version., verification pending label Nov 9, 2023
@samhouts samhouts changed the title .NET 8 DynamicResource not working with gradients [regression/8.0.0] DynamicResource not working with gradients Nov 9, 2023
@gabsamples6
Copy link

We are affected too but this one. @samhouts is this part of any milestone? Inability to work with dynamic resources seems pretty important.
Please advise
thanks

@Felix-Dev
Copy link

Adding my voice here too - we are also impacted by this regression. Using DynamicResource colors in a gradient brush now crashes our app whereas it worked just fine on .NET 7.

When can we expect a fix for this? Thanks.

@faheys
Copy link
Contributor

faheys commented Nov 22, 2023

Also ran into this issue today when upgrading our project to .NET 8!

@Felix-Dev
Copy link

@samhouts @jsuarezruiz @davidortinau Any updates on this?

@Zack-G-I-T
Copy link
Author

@davidortinau This is still a problem. is anyone looking into this? It would be a big task for us to use apptheme binding everywhere and add a "Light" and "Dark" version of each colour to the themes. We would have to add over 500 colors

@developer9969
Copy link

@StephaneDelcroix hi we are affected too.could u please give us an update on this.is this ever going to be fixed.Designers cannot understand why something simple is not supported as we cannot do what they want

@Swodah
Copy link

Swodah commented Dec 21, 2023

Adding voice to this, we are getting binding failures on Dynamic rosource bindings that previously worked:
BackgroundColor="{Binding Source={DynamicResource ColorPrimary}, Mode=OneWay}"
Stroke="{Binding Source={DynamicResource ColorPrimary}, Mode=OneWay}"
this definitely worked in .net 7, but it may have been broken/changed since.

PS: the error description says it cannot convert from DynamicResource to Maui.Graphics.Color or Maui.Controls.brush.

@StephaneDelcroix
Copy link
Contributor

StephaneDelcroix commented Feb 15, 2024

@Swodah

BackgroundColor="{Binding Source={DynamicResource ColorPrimary}, Mode=OneWay}

Bindings are immutable. and DynamicResources are only applied to Element. So I don't know what you expect to work there

@StephaneDelcroix
Copy link
Contributor

StephaneDelcroix commented Feb 15, 2024

@StephaneDelcroix hi we are affected too.could u please give us an update on this.is this ever going to be fixed.Designers cannot understand why something simple is not supported as we cannot do what they want

looking into this, but can not repro on today's main branch

    <Label x:Name="label" Text="Label" >
        <Label.Background>
            <LinearGradientBrush EndPoint="0,1">
                <GradientStop Offset="0.1" Color="{DynamicResource GradientColorStart}" />
                <GradientStop Offset="1.0" Color="{DynamicResource GradientColorEnd}" />
            </LinearGradientBrush>
        </Label.Background>
    </Label>
page.Resources = new ResourceDictionary
{
    ["GradientColorStart"] = Colors.Red,
    ["GradientColorEnd"] = Colors.Blue
};
Assert.That(page.label.Background, Is.TypeOf<LinearGradientBrush>());
var brush = (LinearGradientBrush)page.label.Background;
Assert.That(brush.GradientStops[0].Color, Is.EqualTo(Colors.Red));

page.Resources["GradientColorStart"] = Colors.Green;
Assert.That(brush.GradientStops[0].Color, Is.EqualTo(Colors.Green));

@Zack-G-I-T
Copy link
Author

Zack-G-I-T commented Feb 15, 2024

Hi @StephaneDelcroix , I just tried the project that I linked again and I am still getting the wrong behaviour.
I am using Maui version 8.0.7

I even tried with the nightly "8.0.10-nightly.10055" and still nothing

What version was you using which doesn't replicate?

@developer9969
Copy link

developer9969 commented Feb 16, 2024

@StephaneDelcroix this is still an issue and looking at your test , you are not actually testing the issue but just checking if you can read the colors. that will read the correct colors with the current MergedDictionary

The problem occurs when you switch theme at runtime , the gradient colors set for the light theme in our resource file are different from the one in the dark theme. and this only occurs with gradients.

private void SwitchToLightTheme()
{
    Application.Current.Resources.MergedDictionaries.Remove(DarkTheme);
    Application.Current.Resources.MergedDictionaries.Add(LightTheme);
    Application.Current.UserAppTheme = AppTheme.Light;
}

@StephaneDelcroix
Copy link
Contributor

The unittest tests that DynamicResources works inside gradients, which is the originally reported issue 😄 . Trying your 'let's switch themes' thing

@StephaneDelcroix
Copy link
Contributor

@developer9969 indeed, switching the theme at app level no longer propagate the resources changed event

@developer9969
Copy link

@StephaneDelcroix oh thank you for your prompt reply , yes that is the issue - I just hope it can be sorted as our design team as gone a bit mad with gradients... and users can switch between various themes..

@Zack-G-I-T Zack-G-I-T changed the title [regression/8.0.0] DynamicResource not working with gradients [regression/8.0.0] DynamicResource not working with gradients when switching themes at runtime Feb 16, 2024
rmarinho pushed a commit that referenced this issue Feb 29, 2024
* [C] Propagate resource changes to Background

- fixes #18545

* apply same fix elsewhere
@github-actions github-actions bot locked and limited conversation to collaborators Mar 31, 2024
@samhouts samhouts added this to the .NET 8 SR3 milestone May 15, 2024
@samhouts samhouts added i/regression This issue described a confirmed regression on a currently supported version and removed fixed-in-8.0.14 fixed-in-8.0.40 fixed-in-9.0.0-preview.2.10247 potential-regression This issue described a possible regression on a currently supported version., verification pending labels Jul 1, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-xaml XAML, CSS, Triggers, Behaviors fixed-in-8.0.10 fixed-in-9.0.0-preview.2.10293 i/regression This issue described a confirmed regression on a currently supported version platform/android 🤖 s/triaged Issue has been reviewed s/verified Verified / Reproducible Issue ready for Engineering Triage t/bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

10 participants