Skip to content

Fix: Fixed ThrowInvalidOperationExceptionForMissingInitialization #13785

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Nov 13, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 9 additions & 11 deletions src/Files.App/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@

using CommunityToolkit.WinUI.Helpers;
using CommunityToolkit.WinUI.Notifications;
using Files.App.Helpers;
using Files.App.Services.DateTimeFormatter;
using Files.App.Services.Settings;
using Files.App.Storage.FtpStorage;
using Files.App.Storage.NativeStorage;
using Files.App.UserControls.TabBar;
using Files.App.ViewModels.Settings;
using Files.Core.Services.SizeProvider;
using Files.Core.Storage;
Expand All @@ -22,8 +20,6 @@
using Microsoft.Extensions.Logging;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Media;
using Microsoft.UI.Xaml.Media.Animation;
using Microsoft.Windows.AppLifecycle;
using System.IO;
using System.Text;
Expand Down Expand Up @@ -230,6 +226,10 @@ async Task ActivateAsync()

EnsureSettingsAndConfigurationAreBootstrapped();

// Hook events for the window
MainWindow.Instance.Closed += Window_Closed;
MainWindow.Instance.Activated += Window_Activated;

// TODO: Remove App.Logger instance and replace with DI
Logger = Ioc.Default.GetRequiredService<ILogger<App>>();
Logger.LogInformation($"App launched. Launch args type: {appActivationArguments.Data.GetType().Name}");
Expand Down Expand Up @@ -262,10 +262,6 @@ private void EnsureSuperEarlyWindow()
// Get the MainWindow instance
var window = MainWindow.Instance;

// Hook events for the window
window.Activated += Window_Activated;
window.Closed += Window_Closed;

// Attempt to activate it
window.Activate();
}
Expand Down Expand Up @@ -297,6 +293,8 @@ public void OnActivated(AppActivationArguments activatedEventArgs)
private async void Window_Closed(object sender, WindowEventArgs args)
{
// Save application state and stop any background activity
IUserSettingsService userSettingsService = Ioc.Default.GetRequiredService<IUserSettingsService>();
StatusCenterViewModel satusCenterViewModel = Ioc.Default.GetRequiredService<StatusCenterViewModel>();

// A Workaround for the crash (#10110)
if (LastOpenedFlyout?.IsOpen ?? false)
Expand All @@ -307,15 +305,15 @@ private async void Window_Closed(object sender, WindowEventArgs args)
return;
}

if (Ioc.Default.GetRequiredService<IUserSettingsService>().GeneralSettingsService.LeaveAppRunning &&
if (userSettingsService.GeneralSettingsService.LeaveAppRunning &&
!AppModel.ForceProcessTermination &&
!Process.GetProcessesByName("Files").Any(x => x.Id != Process.GetCurrentProcess().Id))
{
// Close open content dialogs
UIHelpers.CloseAllDialogs();

// Close all notification banners except in progress
Ioc.Default.GetRequiredService<StatusCenterViewModel>().RemoveAllCompletedItems();
satusCenterViewModel.RemoveAllCompletedItems();

// Cache the window instead of closing it
MainWindow.Instance.AppWindow.Hide();
Expand Down