diff --git a/src/Avalonia.Controls/ApplicationLifetimes/ClassicDesktopStyleApplicationLifetime.cs b/src/Avalonia.Controls/ApplicationLifetimes/ClassicDesktopStyleApplicationLifetime.cs index 2c467853d46..7b3de3306b3 100644 --- a/src/Avalonia.Controls/ApplicationLifetimes/ClassicDesktopStyleApplicationLifetime.cs +++ b/src/Avalonia.Controls/ApplicationLifetimes/ClassicDesktopStyleApplicationLifetime.cs @@ -68,7 +68,7 @@ public bool TryShutdown(int exitCode = 0) return DoShutdown(new ShutdownRequestedEventArgs(), true, false, exitCode); } - internal void SetupCore(string[] args) + internal void SubscribeGlobalEvents() { if (_compositeDisposable is not null) { @@ -76,7 +76,7 @@ internal void SetupCore(string[] args) // Until developer started it manually later. To avoid API breaking changes, it will execute Setup method twice. return; } - + _compositeDisposable = new CompositeDisposable( Window.WindowOpenedEvent.AddClassHandler(typeof(Window), (sender, _) => { @@ -91,7 +91,12 @@ internal void SetupCore(string[] args) var window = (Window)sender!; _windows.Remove(window); HandleWindowClosed(window); - })); + })); + } + + internal void SetupCore(string[] args) + { + SubscribeGlobalEvents(); Startup?.Invoke(this, new ControlledApplicationLifetimeStartupEventArgs(args)); @@ -214,6 +219,7 @@ private static ClassicDesktopStyleApplicationLifetime PrepareLifetime(AppBuilder { var lifetime = builder.LifetimeOverride?.Invoke(typeof(ClassicDesktopStyleApplicationLifetime)) as ClassicDesktopStyleApplicationLifetime ?? new ClassicDesktopStyleApplicationLifetime(); + lifetime.SubscribeGlobalEvents(); lifetime.Args = args; lifetimeBuilder?.Invoke(lifetime);