diff --git a/StabilityMatrix.Avalonia/Controls/Dock/DockUserControlBase.cs b/StabilityMatrix.Avalonia/Controls/Dock/DockUserControlBase.cs index 034a6ad3..2191689f 100644 --- a/StabilityMatrix.Avalonia/Controls/Dock/DockUserControlBase.cs +++ b/StabilityMatrix.Avalonia/Controls/Dock/DockUserControlBase.cs @@ -48,26 +48,42 @@ public abstract class DockUserControlBase : DropTargetUserControlBase // Attach handlers for view state saving and loading if (DataContext is InferenceTabViewModelBase vm) { - vm.SaveViewStateRequested += (_, args) => - { - var saveTcs = new TaskCompletionSource(); - - Dispatcher.UIThread.Post(() => - { - var state = new ViewState { DockLayout = SaveDockLayout() }; - saveTcs.SetResult(state); - }); - - args.StateTask ??= saveTcs.Task; - }; - - vm.LoadViewStateRequested += (_, args) => - { - if (args.State.DockLayout is { } layout) - { - LoadDockLayout(layout); - } - }; + vm.SaveViewStateRequested += DataContext_OnSaveViewStateRequested; + vm.LoadViewStateRequested += DataContext_OnLoadViewStateRequested; + } + } + + /// + protected override void OnDetachedFromVisualTree(VisualTreeAttachmentEventArgs e) + { + base.OnDetachedFromVisualTree(e); + + // Detach handlers for view state saving and loading + if (DataContext is InferenceTabViewModelBase vm) + { + vm.SaveViewStateRequested -= DataContext_OnSaveViewStateRequested; + vm.LoadViewStateRequested -= DataContext_OnLoadViewStateRequested; + } + } + + private void DataContext_OnSaveViewStateRequested(object? sender, SaveViewStateEventArgs args) + { + var saveTcs = new TaskCompletionSource(); + + Dispatcher.UIThread.Post(() => + { + var state = new ViewState { DockLayout = SaveDockLayout() }; + saveTcs.SetResult(state); + }); + + args.StateTask ??= saveTcs.Task; + } + + private void DataContext_OnLoadViewStateRequested(object? sender, LoadViewStateEventArgs args) + { + if (args.State.DockLayout is { } layout) + { + LoadDockLayout(layout); } } diff --git a/StabilityMatrix.Avalonia/Views/InferencePage.axaml b/StabilityMatrix.Avalonia/Views/InferencePage.axaml index 59adb39a..5318cd17 100644 --- a/StabilityMatrix.Avalonia/Views/InferencePage.axaml +++ b/StabilityMatrix.Avalonia/Views/InferencePage.axaml @@ -217,6 +217,7 @@