Skip to content

Commit f1d348a

Browse files
authored
Fix: Fixed COMException in MainWindow.EnsureWindowIsInitialized (#15932)
1 parent e7c292d commit f1d348a

File tree

1 file changed

+24
-13
lines changed

1 file changed

+24
-13
lines changed

src/Files.App/MainWindow.xaml.cs

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using Microsoft.UI.Xaml.Controls;
88
using Microsoft.UI.Xaml.Media.Animation;
99
using Microsoft.UI.Xaml.Navigation;
10+
using System.Runtime.InteropServices;
1011
using Windows.ApplicationModel.Activation;
1112
using Windows.Storage;
1213
using WinUIEx;
@@ -58,13 +59,16 @@ public void ShowSplashScreen()
5859
{
5960
var rootFrame = EnsureWindowIsInitialized();
6061

61-
rootFrame.Navigate(typeof(SplashScreenPage));
62+
rootFrame?.Navigate(typeof(SplashScreenPage));
6263
}
6364

6465
public async Task InitializeApplicationAsync(object activatedEventArgs)
6566
{
6667
var rootFrame = EnsureWindowIsInitialized();
6768

69+
if (rootFrame is null)
70+
return;
71+
6872
// Set system backdrop
6973
SystemBackdrop = new AppSystemBackdrop();
7074

@@ -209,22 +213,29 @@ public async Task InitializeApplicationAsync(object activatedEventArgs)
209213
Instance.Restore(); // Restore window if minimized
210214
}
211215

212-
public Frame EnsureWindowIsInitialized()
216+
private Frame? EnsureWindowIsInitialized()
213217
{
214-
// NOTE:
215-
// Do not repeat app initialization when the Window already has content,
216-
// just ensure that the window is active
217-
if (Instance.Content is not Frame rootFrame)
218+
try
218219
{
219-
// Create a Frame to act as the navigation context and navigate to the first page
220-
rootFrame = new() { CacheSize = 1 };
221-
rootFrame.NavigationFailed += OnNavigationFailed;
220+
// NOTE:
221+
// Do not repeat app initialization when the Window already has content,
222+
// just ensure that the window is active
223+
if (Instance.Content is not Frame rootFrame)
224+
{
225+
// Create a Frame to act as the navigation context and navigate to the first page
226+
rootFrame = new() { CacheSize = 1 };
227+
rootFrame.NavigationFailed += OnNavigationFailed;
222228

223-
// Place the frame in the current Window
224-
Instance.Content = rootFrame;
225-
}
229+
// Place the frame in the current Window
230+
Instance.Content = rootFrame;
231+
}
226232

227-
return rootFrame;
233+
return rootFrame;
234+
}
235+
catch (COMException)
236+
{
237+
return null;
238+
}
228239
}
229240

230241
/// <summary>

0 commit comments

Comments
 (0)