|
7 | 7 | using Microsoft.UI.Xaml.Controls; |
8 | 8 | using Microsoft.UI.Xaml.Media.Animation; |
9 | 9 | using Microsoft.UI.Xaml.Navigation; |
| 10 | +using System.Runtime.InteropServices; |
10 | 11 | using Windows.ApplicationModel.Activation; |
11 | 12 | using Windows.Storage; |
12 | 13 | using WinUIEx; |
@@ -58,13 +59,16 @@ public void ShowSplashScreen() |
58 | 59 | { |
59 | 60 | var rootFrame = EnsureWindowIsInitialized(); |
60 | 61 |
|
61 | | - rootFrame.Navigate(typeof(SplashScreenPage)); |
| 62 | + rootFrame?.Navigate(typeof(SplashScreenPage)); |
62 | 63 | } |
63 | 64 |
|
64 | 65 | public async Task InitializeApplicationAsync(object activatedEventArgs) |
65 | 66 | { |
66 | 67 | var rootFrame = EnsureWindowIsInitialized(); |
67 | 68 |
|
| 69 | + if (rootFrame is null) |
| 70 | + return; |
| 71 | + |
68 | 72 | // Set system backdrop |
69 | 73 | SystemBackdrop = new AppSystemBackdrop(); |
70 | 74 |
|
@@ -209,22 +213,29 @@ public async Task InitializeApplicationAsync(object activatedEventArgs) |
209 | 213 | Instance.Restore(); // Restore window if minimized |
210 | 214 | } |
211 | 215 |
|
212 | | - public Frame EnsureWindowIsInitialized() |
| 216 | + private Frame? EnsureWindowIsInitialized() |
213 | 217 | { |
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 |
218 | 219 | { |
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; |
222 | 228 |
|
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 | + } |
226 | 232 |
|
227 | | - return rootFrame; |
| 233 | + return rootFrame; |
| 234 | + } |
| 235 | + catch (COMException) |
| 236 | + { |
| 237 | + return null; |
| 238 | + } |
228 | 239 | } |
229 | 240 |
|
230 | 241 | /// <summary> |
|
0 commit comments