You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When interacting with the ComboBox (that has a Tag), specifically when clicking it, an exception is thrown (System.InvalidOperationException, The control already has a visual parent), resulting in a crash in the application.
The ComboBox named ThemeComboBox contains a list of Fluent theme options. When a theme option is selected, the ThemeComboBox_SelectionChanged event handler is triggered. This handler retrieves the selected theme key from the ComboBoxItem's Tag property and passes it to the ChangePalette method. This method dynamically changes the application's Fluent theme based on the selected theme key, updating the UI to reflect the new theme.
For some reason, it works as intended in the Designer/Preview in VS Code, but doesn't work on the running app.
In Preview/Designer:
In Running Program:
To Reproduce
You need the FluentAvalonia and reproduce this in Windows Desktop.
Im using Win10 Home for this.
You just need to update the current FluentTheme in the App and interact with a ComboBox that has a Tag in it.
It will throw the exception:
Excepción producida: 'System.InvalidOperationException' en Avalonia.Base.dll
Excepción no controlada del tipo 'System.InvalidOperationException' en Avalonia.Base.dll
The control already has a visual parent.
Expected behavior
Changes theme and is able to interact with the ComboBox again, as it does in the Designer/Preview of the MainView.axaml
Avalonia version
11.0.2
OS
Windows
Additional context
Code if needed:
In MainView.axaml:
<UserControlxmlns="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"xmlns:vm="clr-namespace:UnoGisDesktopAndrew.ViewModels"xmlns:views="clr-namespace:UnoGisDesktopAndrew.Views"mc:Ignorable="d"d:DesignWidth="800"d:DesignHeight="500"x:Class="UnoGisDesktopAndrew.Views.MainView"x:DataType="vm:MainViewModel">
<Design.DataContext>
<!-- This only sets the DataContext for the previewer in an IDE, to set the actual DataContext for runtime, set the DataContext property in code (look at App.axaml.cs) -->
<vm:MainViewModel />
</Design.DataContext>
<DockPanel >
<Panel DockPanel.Dock="Left">
<StackPanelName="ButtonPanel"Background="{DynamicResource SystemAccentColor}">
<ButtonName="ControlDisplay"Content="ControlDisplay"HorizontalAlignment="Stretch"CommandParameter="ControlDisplayView"/>
<ButtonName="XY"Content="XY"HorizontalAlignment="Stretch"CommandParameter="XYView"/>
<ButtonName="Increment"Content="Increment"HorizontalAlignment="Stretch"CommandParameter="IncrementView"/>
<ButtonName="Locations"Content="Locations"HorizontalAlignment="Stretch"CommandParameter="LocationsView"/>
<ButtonName="Find"Content="Find"HorizontalAlignment="Stretch"CommandParameter="FindView"/>
<ButtonName="Attachments"Content="Attachments"HorizontalAlignment="Stretch"CommandParameter="AttachmentsView"/>
<ButtonName="HistoricalDisplay"Content="HistoricalDisplay"HorizontalAlignment="Stretch"CommandParameter="HistoricalDisplayView"/>
<ButtonName="HistoricalDisplayDialog"Content="HistoricalDisplayDialog"HorizontalAlignment="Stretch"CommandParameter="HistoricalDisplayDialog"/>
</StackPanel>
<Panel DockPanel.Dock="Left"VerticalAlignment="Bottom">
<ToggleButtonName="ActualThemeToggleButton">
<PathIconName="ActualThemeIcon"Data="M20.026 17.001c-2.762 4.784-8.879 6.423-13.663 3.661A9.965 9.965 0 0 1 3.13 17.68a.75.75 0 0 1 .365-1.132c3.767-1.348 5.785-2.91 6.956-5.146 1.232-2.353 1.551-4.93.689-8.463a.75.75 0 0 1 .769-.927 9.961 9.961 0 0 1 4.457 1.327c4.784 2.762 6.423 8.879 3.66 13.662Z"/>
</ToggleButton>
<!-- Here is the relevant component -->
<ComboBoxName="ThemeComboBox"HorizontalAlignment="Right"SelectionChanged="ThemeComboBox_SelectionChanged"Margin="10">
<ComboBoxItemContent="Lavander"Tag="Lavander"/>
<ComboBoxItemContent="Forest"Tag="Forest"/>
<ComboBoxItemContent="NightTime"Tag="NightTime"/>
<ComboBoxItemContent="ToxicGreen"Tag="ToxicGreen"/>
</ComboBox>
</Panel>
</Panel>
<PanelName="ViewsPanel">
<Panel.Styles>
<StyleSelector="UserControl">
<SetterProperty="BorderBrush"Value="Black"/>
</Style>
</Panel.Styles>
<!-- <views:ControlDisplayView Name="ControlDisplayView" Padding="10"/> <views:XYView Name="XYView" Height="450" Width="300" Padding="10" BorderBrush="{DynamicResource SystemAccentColor}" BorderThickness="1"/> <views:IncrementView Name="IncrementView" Width="300" Padding="10" BorderBrush="{DynamicResource SystemAccentColor}" BorderThickness="1"/> <views:LocationsView Name="LocationsView" Width="300" Padding="10" BorderBrush="{DynamicResource SystemAccentColor}" BorderThickness="1"/> <views:FindView Name="FindView" Height="150" Width="300" Padding="10" BorderBrush="{DynamicResource SystemAccentColor}" BorderThickness="1"/> <views:AttachmentsView Name="AttachmentsView" Height="150" Width="300" Padding="10" BorderBrush="{DynamicResource SystemAccentColor}" BorderThickness="1" Margin="0 150"/> <views:HistoricalDisplayView Name="HistoricalDisplayView" Height="200" Width="300" Padding="10" BorderBrush="{DynamicResource SystemAccentColor}" BorderThickness="1" Margin="0 80"/>-->
</Panel>
</DockPanel>
</UserControl>
<Applicationxmlns="https://github.com/avaloniaui"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"x:Class="UnoGisDesktopAndrew.App"RequestedThemeVariant="Dark">
<!-- "Default" ThemeVariant follows system theme variant. "Dark" or "Light" are other available options. -->
<Application.Styles>
<FluentTheme/>
</Application.Styles>
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceIncludeSource="avares://UnoGisDesktopAndrew/FluentPalettes.axaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
In App.axaml.cs:
using Avalonia;using Avalonia.Controls;using Avalonia.Controls.ApplicationLifetimes;using Avalonia.Markup.Xaml;using Avalonia.Styling;using Avalonia.Themes.Fluent;using System;using System.Diagnostics;using UnoGisDesktopAndrew.ViewModels;using UnoGisDesktopAndrew.Views;namespace UnoGisDesktopAndrew;publicpartialclassApp:Application{publicoverridevoidInitialize(){
AvaloniaXamlLoader.Load(this);}publicoverridevoidOnFrameworkInitializationCompleted(){if(ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop){
desktop.MainWindow =new MainWindow
{DataContext=new MainViewModel()};}elseif(ApplicationLifetime is ISingleViewApplicationLifetime singleViewPlatform){
singleViewPlatform.MainView =new MainView
{DataContext=new MainViewModel()};}base.OnFrameworkInitializationCompleted();}publicvoidToggleThemeVariant(){if(this.RequestedThemeVariant == ThemeVariant.Light){RequestedThemeVariant= ThemeVariant.Dark;}else{RequestedThemeVariant= ThemeVariant.Light;}}publicvoidChangeFluentTheme(stringthemeKey){// Get the resource dictionary containing the themesvarthemeDictionary= Resources.MergedDictionaries[0]as ResourceDictionary;if(themeDictionary==null){thrownew InvalidOperationException("Theme resource dictionary not found.");}// Retrieve the selected theme from the resource dictionaryvarselectedTheme= themeDictionary[themeKey]as FluentTheme;if(selectedTheme==null){thrownew InvalidOperationException($"Theme with key {themeKey} not found.");}// Clear the current styles
Styles.Clear();// Add the selected theme to the application's styles
Styles.Add(selectedTheme);// Refresh the theme variant to apply changesRequestedThemeVariant=RequestedThemeVariant;}}
Theming doesn't work well with replacing whole themes, as they include control templates replacement as well, forcing whole app UI to be rebuilt. At some point it crashes, when same child control was re-attached to another template parent.
Replacing ComboBox ComboBoxItem with ItemsSource would help in your case. But in general, I wouldn't suggest replacing FluentTheme in runtime for production apps (might be fine for a demo).
In ControlCatalog we reopen whole main window, when we switch full theme.
Describe the bug
When interacting with the ComboBox (that has a Tag), specifically when clicking it, an exception is thrown (System.InvalidOperationException, The control already has a visual parent), resulting in a crash in the application.
The ComboBox named ThemeComboBox contains a list of Fluent theme options. When a theme option is selected, the ThemeComboBox_SelectionChanged event handler is triggered. This handler retrieves the selected theme key from the ComboBoxItem's Tag property and passes it to the ChangePalette method. This method dynamically changes the application's Fluent theme based on the selected theme key, updating the UI to reflect the new theme.
For some reason, it works as intended in the Designer/Preview in VS Code, but doesn't work on the running app.
In Preview/Designer:
![2024-05-23-11-09-37-ezgif com-optimize](https://private-user-images.githubusercontent.com/128989944/333126951-92c92fa3-da3d-4247-b872-30006314dd55.gif?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjI2NjkxMzAsIm5iZiI6MTcyMjY2ODgzMCwicGF0aCI6Ii8xMjg5ODk5NDQvMzMzMTI2OTUxLTkyYzkyZmEzLWRhM2QtNDI0Ny1iODcyLTMwMDA2MzE0ZGQ1NS5naWY_WC1BbXotQWxnb3JpdGhtPUFXUzQtSE1BQy1TSEEyNTYmWC1BbXotQ3JlZGVudGlhbD1BS0lBVkNPRFlMU0E1M1BRSzRaQSUyRjIwMjQwODAzJTJGdXMtZWFzdC0xJTJGczMlMkZhd3M0X3JlcXVlc3QmWC1BbXotRGF0ZT0yMDI0MDgwM1QwNzA3MTBaJlgtQW16LUV4cGlyZXM9MzAwJlgtQW16LVNpZ25hdHVyZT1jYzY2OWYyMjA3NzRmNzcxZDE2ODY2Mzg3YjJkYTQ3NGYyNDA1OWNmOTJmYzQzZjUxZTQ1NjE5MGNjMTUxZTg2JlgtQW16LVNpZ25lZEhlYWRlcnM9aG9zdCZhY3Rvcl9pZD0wJmtleV9pZD0wJnJlcG9faWQ9MCJ9.vvbn2bZL83UsNonNghS3PX56dxvWdpbbEo8seCY2UsY)
In Running Program:
![2024-05-23-11-14-29-ezgif com-optimize](https://private-user-images.githubusercontent.com/128989944/333126421-f21aef70-c470-48a5-9ae5-c9fac4450082.gif?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjI2NjkxMzAsIm5iZiI6MTcyMjY2ODgzMCwicGF0aCI6Ii8xMjg5ODk5NDQvMzMzMTI2NDIxLWYyMWFlZjcwLWM0NzAtNDhhNS05YWU1LWM5ZmFjNDQ1MDA4Mi5naWY_WC1BbXotQWxnb3JpdGhtPUFXUzQtSE1BQy1TSEEyNTYmWC1BbXotQ3JlZGVudGlhbD1BS0lBVkNPRFlMU0E1M1BRSzRaQSUyRjIwMjQwODAzJTJGdXMtZWFzdC0xJTJGczMlMkZhd3M0X3JlcXVlc3QmWC1BbXotRGF0ZT0yMDI0MDgwM1QwNzA3MTBaJlgtQW16LUV4cGlyZXM9MzAwJlgtQW16LVNpZ25hdHVyZT01NDdlM2QzM2Y5NTA4OTFhZjdlZTZhMTU3MWM3ODEyOWNjNzljMjQ2ZmRlNDk0YjY0NTlkOTA1ZDEyYmJiMzY3JlgtQW16LVNpZ25lZEhlYWRlcnM9aG9zdCZhY3Rvcl9pZD0wJmtleV9pZD0wJnJlcG9faWQ9MCJ9.HWHp8bK3XP0KHWWBtPi3CmVMrsl_IUl1fteebEebY6Y)
To Reproduce
You need the FluentAvalonia and reproduce this in Windows Desktop.
Im using Win10 Home for this.
You just need to update the current FluentTheme in the App and interact with a ComboBox that has a Tag in it.
It will throw the exception:
Excepción producida: 'System.InvalidOperationException' en Avalonia.Base.dll
Excepción no controlada del tipo 'System.InvalidOperationException' en Avalonia.Base.dll
The control already has a visual parent.
Expected behavior
Changes theme and is able to interact with the ComboBox again, as it does in the Designer/Preview of the MainView.axaml
Avalonia version
11.0.2
OS
Windows
Additional context
Code if needed:
In MainView.axaml:
In MainView.axaml.cs
In App.axaml:
In App.axaml.cs:
In FluentPalletes.axaml:
The text was updated successfully, but these errors were encountered: