unoplatform / uno

Build Mobile, Desktop and WebAssembly apps with C# and XAML. Today. Open source and professionally supported.
https://platform.uno
Apache License 2.0
8.77k stars 707 forks source link

Hyperlink Foreground not changing color when changing for a theme different than the default in place #14911

Open agneszitte opened 9 months ago

agneszitte commented 9 months ago

Current behavior

Hyperlink Foreground not changing color when changing for a theme different than the default in place In my example here, my default theme is Light but if it was Dark the result would be reversed

Expected behavior

Hyperlink Foreground should change color properly when changing theme like in a WinUI app (see the WinUI sample app in attachment here HyperlinkChangeThemeTest.zip in comparison with the same snippet of code mentioned below tested in an Uno app)

How to reproduce it (as minimally and precisely as possible)

Test with this snippet of code while testing with RequestedTheme="Light" and RequestedTheme="Dark".

  <StackPanel Orientation="Horizontal"
              HorizontalAlignment="Center"
              VerticalAlignment="Center">
    <StackPanel.Resources>
      <ResourceDictionary>
        <ResourceDictionary.ThemeDictionaries>
          <ResourceDictionary x:Key="Light">
            <SolidColorBrush x:Key="myBrush"
                             Color="DeepPink" />
          </ResourceDictionary>
          <ResourceDictionary x:Key="Dark">
            <SolidColorBrush x:Key="myBrush"
                             Color="Cyan" />
          </ResourceDictionary>
        </ResourceDictionary.ThemeDictionaries>
      </ResourceDictionary>
    </StackPanel.Resources>

      <TextBlock Foreground="{ThemeResource myBrush}"
                 TextWrapping="Wrap">
                <Span>
                    Test changing theme with an (<Hyperlink Foreground="{ThemeResource myBrush}" NavigateUri="https://platform.uno/">Uno Platform</Hyperlink>) sample <Italic>App</Italic>.
                </Span>
      </TextBlock>
  </StackPanel>

Notice the Foreground color is not updating for the theme that is different that your current default one.

Workaround

N/A

Works on UWP/WinUI

Yes (for a WinUI sample app for example) / No (for the Windows platform with an Uno Platform sample app)

Environment

Uno.UI / Uno.UI.WebAssembly / Uno.UI.Skia

NuGet package version(s)

5.1.0-dev.1139

Affected platforms

WebAssembly, Android, iOS, Skia (GTK on Linux/macOS/Windows), Windows

IDE

Visual Studio 2022

IDE version

Version 17.8.1

Relevant plugins

N/A

Anything else we need to know?

The WinUi sample app does not have the issue but the Uno sample app yes, even for the Windows platform when changing for a theme different than the default in place

agneszitte commented 9 months ago

fyi @jeromelaban, @MartinZikmund

Youssef1313 commented 8 months ago

This is not working for me in a pure WinUI app.

<?xml version="1.0" encoding="utf-8"?>
<Window
    x:Class="App12.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:App12"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <StackPanel Orientation="Horizontal"
              HorizontalAlignment="Center"
              VerticalAlignment="Center" x:Name="sp">
        <StackPanel.Resources>
            <ResourceDictionary>
                <ResourceDictionary.ThemeDictionaries>
                    <ResourceDictionary x:Key="Light">
                        <SolidColorBrush x:Key="myBrush"
                             Color="DeepPink" />
                    </ResourceDictionary>
                    <ResourceDictionary x:Key="Dark">
                        <SolidColorBrush x:Key="myBrush"
                             Color="Cyan" />
                    </ResourceDictionary>
                </ResourceDictionary.ThemeDictionaries>
            </ResourceDictionary>
        </StackPanel.Resources>

        <TextBlock Foreground="{ThemeResource myBrush}"
                 TextWrapping="Wrap">
                <Span>
                    Test changing theme with an (<Hyperlink Foreground="{ThemeResource myBrush}" NavigateUri="https://platform.uno/">Uno Platform</Hyperlink>) sample <Italic>App</Italic>.
                </Span>
        </TextBlock>
        <Button Content="Click" Click="OnClick" />
    </StackPanel>
</Window>
using Microsoft.UI.Xaml;

namespace App12;

public sealed partial class MainWindow : Window
{
    public MainWindow()
    {
        this.InitializeComponent();
    }

    private void OnClick(object sender, RoutedEventArgs e)
    {
        if (this.sp.XamlRoot.Content is FrameworkElement content)
            content.RequestedTheme = content.RequestedTheme == ElementTheme.Dark ? ElementTheme.Light : ElementTheme.Dark;
    }
}
iurycarlos commented 7 months ago

More info:

Working fine on Windows + Material. Doesn't work for Skia nor WASM.

Changing RequestedTheme only works for Window.Current.Content:

((FrameworkElement)Window.Current.Content).RequestedTheme = ElementTheme.Dark; //dark as an exemple

Any other control will not take effect.

MartinZikmund commented 7 months ago

@iurycarlos that is intended, as element level theme is not supported yet, so it can only be set on the XamlRoot.Content == Window.Content level. Taking that into account, is this issue not relevant? https://github.com/unoplatform/uno/issues/3302

iurycarlos commented 7 months ago

Oh! it is completely relevant. Actually both issues have the same subject

MartinZikmund commented 7 months ago

In that case this one can probably only be fixed after #3302