Avalonia icon indicating copy to clipboard operation
Avalonia copied to clipboard

Changing theme variant at runtime causes Run inside a Textblock to not always render

Open Kryptos-FR opened this issue 6 months ago • 0 comments

Describe the bug

When the theme variant is changed (e.g. from Dark to Light) either by code or by OS settings, the runs inside a textblock sometimes (not always) don't render.

In the MRE below the bug doesn't always manifest itself. In bigger apps (with more to render on the screen) it is more frequent.

To Reproduce

Here is a minimal reproducible example (MRE).

AvaloniaThemeVariantIssue.csproj
<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>WinExe</OutputType>
    <TargetFramework>net8.0</TargetFramework>
    <Nullable>enable</Nullable>
    <BuiltInComInteropSupport>true</BuiltInComInteropSupport>
    <ApplicationManifest>app.manifest</ApplicationManifest>
    <AvaloniaUseCompiledBindingsByDefault>true</AvaloniaUseCompiledBindingsByDefault>
  </PropertyGroup>
  <PropertyGroup>
    <AvaloniaVersion>11.3.1</AvaloniaVersion>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Avalonia" Version="$(AvaloniaVersion)" />
    <PackageReference Include="Avalonia.Desktop" Version="$(AvaloniaVersion)" />
    <PackageReference Include="Avalonia.Themes.Simple" Version="$(AvaloniaVersion)" />
    <PackageReference Include="Avalonia.Themes.Fluent" Version="$(AvaloniaVersion)" />
    <PackageReference Include="Avalonia.Fonts.Inter" Version="$(AvaloniaVersion)" />
    <!--Condition below is needed to remove Avalonia.Diagnostics package from build output in Release configuration.-->
    <PackageReference Condition="'$(Configuration)' == 'Debug'" Include="Avalonia.Diagnostics" Version="$(AvaloniaVersion)" />
  </ItemGroup>
</Project>
App.xaml
<Application xmlns="https://github.com/avaloniaui"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             x:Class="AvaloniaThemeVariantIssue.App"
             RequestedThemeVariant="Default">
  <!-- "Default" ThemeVariant follows system theme variant. "Dark" or "Light" are other available options. -->
  <Application.Styles>
    <!--<SimpleTheme/>-->
    <FluentTheme />
  </Application.Styles>
</Application>
MainWindow.axaml
<Window xmlns="https://github.com/avaloniaui"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
        x:Class="AvaloniaThemeVariantIssue.MainWindow"
        Title="AvaloniaThemeVariantIssue"
        Background="{DynamicResource CustomBackgroundBrush}">
  <Window.Resources>
    <ResourceDictionary>
      <ResourceDictionary.ThemeDictionaries>
        <ResourceDictionary x:Key="Default">
          <SolidColorBrush x:Key="CustomBackgroundBrush" Color="AliceBlue"/>
        </ResourceDictionary>
        <ResourceDictionary x:Key="Dark">
          <SolidColorBrush x:Key="CustomBackgroundBrush" Color="MidnightBlue"/>
        </ResourceDictionary>
      </ResourceDictionary.ThemeDictionaries>
    </ResourceDictionary>
  </Window.Resources>
  <DockPanel>
    <Button DockPanel.Dock="Top"
            Content="Toggle variant"
            HorizontalAlignment="Left"
            Click="Button_OnClick"/>
    <StackPanel Orientation="Vertical">
      <TextBlock Text="This is inside a textblock."/>
      <TextBlock>
        <Run Text="Those"/>
        <Run Text="are"/>
        <Run Text="runs."/>
      </TextBlock>
    </StackPanel>
  </DockPanel>
</Window>
MainWindow.axaml.cs
using Avalonia;
using Avalonia.Controls;
using Avalonia.Interactivity;
using Avalonia.Styling;

namespace AvaloniaThemeVariantIssue;

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

	private void Button_OnClick(object? sender, RoutedEventArgs e)
	{
		if (Application.Current is null) return;

		switch (Application.Current.ActualThemeVariant.Key)
		{
			case nameof(ThemeVariant.Dark):
				Application.Current.RequestedThemeVariant = ThemeVariant.Light;
				break;
			case nameof(ThemeVariant.Light):
				Application.Current.RequestedThemeVariant = ThemeVariant.Dark;
				break;
		}
	}
}

To reproduce, spam the "Toggle variant" button until the second text disappear.

Expected behavior

Changing the theme variant should not prevent rendering of parts of the UI.

Avalonia version

11.3.1

OS

Windows

Additional context

This is a regression in 11.3.1. I wasn't able to reproduce from version 11.0.0 to version 11.3.0.

This issue only seems to occur with the Fluent theme. I wasn't able to reproduce with the Simple theme, but it might be because the theme is simpler and the MRE doesn't have much to render.

Integrating the code of the MRE into a bigger project makes it easier to reproduce.

Kryptos-FR avatar Jun 18 '25 15:06 Kryptos-FR