diff --git a/src/Files.App/Views/Layouts/BaseGroupableLayoutPage.cs b/src/Files.App/Views/Layouts/BaseGroupableLayoutPage.cs index 2d867c252741..62de8527bb8e 100644 --- a/src/Files.App/Views/Layouts/BaseGroupableLayoutPage.cs +++ b/src/Files.App/Views/Layouts/BaseGroupableLayoutPage.cs @@ -171,10 +171,20 @@ await Task.WhenAll(ParentShellPageInstance.SlimContentPage.SelectedItems.Select( protected virtual void ItemManipulationModel_FocusFileListInvoked(object? sender, EventArgs e) { - var focusedElement = (FrameworkElement)FocusManager.GetFocusedElement(MainWindow.Instance.Content.XamlRoot); - var isFileListFocused = DependencyObjectHelpers.FindParent(focusedElement) == ItemsControl; - if (!isFileListFocused) - ListViewBase.Focus(FocusState.Programmatic); + try + { + if (App.AppModel.IsMainWindowClosed) + return; + + var focusedElement = (FrameworkElement)FocusManager.GetFocusedElement(MainWindow.Instance.Content.XamlRoot); + var isFileListFocused = DependencyObjectHelpers.FindParent(focusedElement) == ItemsControl; + if (!isFileListFocused) + ListViewBase.Focus(FocusState.Programmatic); + } + catch + { + // Handle exception in case the window is closed during the operation + } } protected virtual void ItemManipulationModel_SelectAllItemsInvoked(object? sender, EventArgs e) diff --git a/src/Files.App/Views/MainPage.xaml.cs b/src/Files.App/Views/MainPage.xaml.cs index c6a382b60d51..d17335dcd6ac 100644 --- a/src/Files.App/Views/MainPage.xaml.cs +++ b/src/Files.App/Views/MainPage.xaml.cs @@ -171,7 +171,7 @@ public async void MultitaskingControl_CurrentInstanceChanged(object? sender, Cur // Focus the content of the selected tab item (this also avoids an issue where the Omnibar sometimes steals the focus) await Task.Delay(100); - if (ContentPageContext?.ShellPage?.PaneHolder != null) + if (!App.AppModel.IsMainWindowClosed && ContentPageContext?.ShellPage?.PaneHolder != null) ContentPageContext.ShellPage.PaneHolder.FocusActivePane(); }