From 79f29ccae7dee92c0566986b1592def497cfbc72 Mon Sep 17 00:00:00 2001 From: Jonathan Gilbert Date: Thu, 27 Jun 2024 21:37:53 -0500 Subject: [PATCH] Fix TrayIcon menu crash (#16024) * Updated the TrayIcon in IntegrationTestApp to have an associated menu. Updated App.axaml.cs to set the DataContext for binding and provide a command implementation. Added a project reference to Avalonia.ReactiveUI. * Updated CanExecuteChanged in NativeMenuItem.cs to thunk to the UI thread if it ends up running on a non-UI thread. * Replace ReactiveUI with MiniMvvm to simplify integration tests app --------- Co-authored-by: Max Katz --- samples/IntegrationTestApp/App.axaml | 22 +++++++++++++------ samples/IntegrationTestApp/App.axaml.cs | 16 ++++++++++++++ .../IntegrationTestApp.csproj | 1 + src/Avalonia.Controls/NativeMenuItem.cs | 6 ++++- 4 files changed, 37 insertions(+), 8 deletions(-) diff --git a/samples/IntegrationTestApp/App.axaml b/samples/IntegrationTestApp/App.axaml index 7079074d64..f23ce53dbe 100644 --- a/samples/IntegrationTestApp/App.axaml +++ b/samples/IntegrationTestApp/App.axaml @@ -1,13 +1,21 @@ + xmlns:self="using:IntegrationTestApp" + x:Class="IntegrationTestApp.App" + x:DataType="self:App" + RequestedThemeVariant="Default"> - - - - - - + + + + + + + + + + + diff --git a/samples/IntegrationTestApp/App.axaml.cs b/samples/IntegrationTestApp/App.axaml.cs index 022931366d..59f1185e17 100644 --- a/samples/IntegrationTestApp/App.axaml.cs +++ b/samples/IntegrationTestApp/App.axaml.cs @@ -1,11 +1,25 @@ +using System.Windows.Input; + using Avalonia; +using Avalonia.Controls; using Avalonia.Controls.ApplicationLifetimes; using Avalonia.Markup.Xaml; +using MiniMvvm; namespace IntegrationTestApp { public class App : Application { + public App() + { + ShowWindowCommand = MiniCommand.Create(() => + { + var window = new Window() { Title = "TrayIcon demo window" }; + window.Show(); + }); + DataContext = this; + } + public override void Initialize() { AvaloniaXamlLoader.Load(this); @@ -20,5 +34,7 @@ namespace IntegrationTestApp base.OnFrameworkInitializationCompleted(); } + + public ICommand ShowWindowCommand { get; } } } diff --git a/samples/IntegrationTestApp/IntegrationTestApp.csproj b/samples/IntegrationTestApp/IntegrationTestApp.csproj index 587cc98d32..e1bfd6e7b1 100644 --- a/samples/IntegrationTestApp/IntegrationTestApp.csproj +++ b/samples/IntegrationTestApp/IntegrationTestApp.csproj @@ -25,6 +25,7 @@ + diff --git a/src/Avalonia.Controls/NativeMenuItem.cs b/src/Avalonia.Controls/NativeMenuItem.cs index 5c7c4242b9..6c093ff1f6 100644 --- a/src/Avalonia.Controls/NativeMenuItem.cs +++ b/src/Avalonia.Controls/NativeMenuItem.cs @@ -5,6 +5,7 @@ using Avalonia.Controls.Primitives; using Avalonia.Input; using Avalonia.Media.Imaging; using Avalonia.Metadata; +using Avalonia.Threading; using Avalonia.Utilities; namespace Avalonia.Controls @@ -164,7 +165,10 @@ namespace Avalonia.Controls void CanExecuteChanged() { - SetCurrentValue(IsEnabledProperty, Command?.CanExecute(CommandParameter) ?? true); + if (!Dispatcher.UIThread.CheckAccess()) + Dispatcher.UIThread.Invoke(() => CanExecuteChanged()); + else + SetCurrentValue(IsEnabledProperty, Command?.CanExecute(CommandParameter) ?? true); } public bool HasClickHandlers => Click != null;