unoplatform / uno.extensions

Libraries to ease common developer tasks associated with building multi-platform mobile, desktop and web applications using Uno Platform or WinAppSDK.
https://platform.uno/
Other
73 stars 47 forks source link

[MVUX/CSM] Resources don't update after changing Fluent Theme manually #2429

Open morning4coffe-dev opened 3 months ago

morning4coffe-dev commented 3 months ago

Changing the theme manually with a ToggleSwitch inside the app results in the app not updating its background on Windows and on other platforms, the color is too dark when in DarkTheme.

Tested on my side on "Uno.Sdk": "5.2.175"

Repro with my notes: https://github.com/morning4coffe-dev/uno-platform-testing/tree/bug/mvux-csm-fluent-theme-resource-update

Then testing the same case in XAML+MVVM project, I am not able to repro this issue.

Originally posted by @kucint in https://github.com/unoplatform/uno/discussions/17487:

Changing Theme to dark and back to light works fine on Desktop. On Windows, controls seem to react correctly on change, except the main frame background which stays always light.

    public MainPage() => this
        .DataContext(new BindableMainPageModel(), (page, vm) => page
        .Background(ThemeResource.Get<Brush>("ApplicationPageBackgroundThemeBrush"))   // -> does not work on windows
        .Content(
            new ToggleSwitch()
            .OnContent("Dark mode")
            .OffContent("Light mode")
            .Name(out ToggleSwitch c, c => c.Toggled += (s, e) =>
            {
                FrameworkElement? content = XamlRoot?.Content as FrameworkElement;
                if (content is not null)
                    content.RequestedTheme = content.RequestedTheme == ElementTheme.Dark ? ElementTheme.Light : ElementTheme.Dark;
            })
            .IsOn(x => x.Binding(() => vm.IsAppInDarkMode).TwoWay())
        ));

Model:

internal partial record MainPageModel
{
    public IState<bool> IsAppInDarkMode => State<bool>.Value(this, () => false);
}

image

image

[!NOTE] Latest dev branch used: "Uno.Sdk": "5.4.0-dev.99"

agneszitte commented 3 months ago

@kazo0 to verify if it is a duplicate: https://github.com/unoplatform/uno.csharpmarkup/issues/716

kazo0 commented 3 months ago

@agneszitte yes this is the same issue as https://github.com/unoplatform/uno.csharpmarkup/issues/716

MartinZikmund commented 1 month ago

To work around this issue, you can create a Style targeting Page, with a setter for the Background (using ThemeResource in the Setter) - theme resources should be correctly applied and observed when part of Style, and this should work across all targets including WinUI

MartinZikmund commented 1 month ago

Workaround similar to https://github.com/microsoft/microsoft-ui-xaml/issues/9464#issuecomment-2258432746

MartinZikmund commented 1 month ago

Created https://github.com/microsoft/microsoft-ui-xaml/issues/10029