Skip to content

Commit 6696e79

Browse files
authored
Fix: Fixed ThrowInvalidOperationExceptionForMissingInitialization (#13785)
1 parent 220f6a4 commit 6696e79

File tree

1 file changed

+9
-11
lines changed

1 file changed

+9
-11
lines changed

src/Files.App/App.xaml.cs

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,10 @@
33

44
using CommunityToolkit.WinUI.Helpers;
55
using CommunityToolkit.WinUI.Notifications;
6-
using Files.App.Helpers;
76
using Files.App.Services.DateTimeFormatter;
87
using Files.App.Services.Settings;
98
using Files.App.Storage.FtpStorage;
109
using Files.App.Storage.NativeStorage;
11-
using Files.App.UserControls.TabBar;
1210
using Files.App.ViewModels.Settings;
1311
using Files.Core.Services.SizeProvider;
1412
using Files.Core.Storage;
@@ -22,8 +20,6 @@
2220
using Microsoft.Extensions.Logging;
2321
using Microsoft.UI.Xaml;
2422
using Microsoft.UI.Xaml.Controls;
25-
using Microsoft.UI.Xaml.Media;
26-
using Microsoft.UI.Xaml.Media.Animation;
2723
using Microsoft.Windows.AppLifecycle;
2824
using System.IO;
2925
using System.Text;
@@ -230,6 +226,10 @@ async Task ActivateAsync()
230226

231227
EnsureSettingsAndConfigurationAreBootstrapped();
232228

229+
// Hook events for the window
230+
MainWindow.Instance.Closed += Window_Closed;
231+
MainWindow.Instance.Activated += Window_Activated;
232+
233233
// TODO: Remove App.Logger instance and replace with DI
234234
Logger = Ioc.Default.GetRequiredService<ILogger<App>>();
235235
Logger.LogInformation($"App launched. Launch args type: {appActivationArguments.Data.GetType().Name}");
@@ -262,10 +262,6 @@ private void EnsureSuperEarlyWindow()
262262
// Get the MainWindow instance
263263
var window = MainWindow.Instance;
264264

265-
// Hook events for the window
266-
window.Activated += Window_Activated;
267-
window.Closed += Window_Closed;
268-
269265
// Attempt to activate it
270266
window.Activate();
271267
}
@@ -297,6 +293,8 @@ public void OnActivated(AppActivationArguments activatedEventArgs)
297293
private async void Window_Closed(object sender, WindowEventArgs args)
298294
{
299295
// Save application state and stop any background activity
296+
IUserSettingsService userSettingsService = Ioc.Default.GetRequiredService<IUserSettingsService>();
297+
StatusCenterViewModel satusCenterViewModel = Ioc.Default.GetRequiredService<StatusCenterViewModel>();
300298

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

310-
if (Ioc.Default.GetRequiredService<IUserSettingsService>().GeneralSettingsService.LeaveAppRunning &&
308+
if (userSettingsService.GeneralSettingsService.LeaveAppRunning &&
311309
!AppModel.ForceProcessTermination &&
312310
!Process.GetProcessesByName("Files").Any(x => x.Id != Process.GetCurrentProcess().Id))
313311
{
314312
// Close open content dialogs
315313
UIHelpers.CloseAllDialogs();
316-
314+
317315
// Close all notification banners except in progress
318-
Ioc.Default.GetRequiredService<StatusCenterViewModel>().RemoveAllCompletedItems();
316+
satusCenterViewModel.RemoveAllCompletedItems();
319317

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

0 commit comments

Comments
 (0)