|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT license. See LICENSE file in the project root for full license information. |
| 3 | + |
| 4 | +using System; |
| 5 | + |
| 6 | +using Windows.ApplicationModel; |
| 7 | +using Windows.ApplicationModel.Activation; |
| 8 | +using Windows.UI.Xaml; |
| 9 | +using Windows.UI.Xaml.Controls; |
| 10 | +using Windows.UI.Xaml.Navigation; |
| 11 | + |
| 12 | +namespace BlankUwpNet9App; |
| 13 | + |
| 14 | +/// <summary> |
| 15 | +/// Provides application-specific behavior to supplement the default Application class. |
| 16 | +/// </summary> |
| 17 | +public sealed partial class App : Application |
| 18 | +{ |
| 19 | + /// <summary> |
| 20 | + /// Initializes the singleton application object. This is the first line of authored code |
| 21 | + /// executed, and as such is the logical equivalent of main() or WinMain(). |
| 22 | + /// </summary> |
| 23 | + public App() |
| 24 | + { |
| 25 | + InitializeComponent(); |
| 26 | + |
| 27 | + Suspending += OnSuspending; |
| 28 | + } |
| 29 | + |
| 30 | + /// <inheritdoc/> |
| 31 | + protected override void OnLaunched(LaunchActivatedEventArgs args) |
| 32 | + { |
| 33 | + var rootFrame = Window.Current.Content as Frame; |
| 34 | + |
| 35 | + // Do not repeat app initialization when the Window already has content, |
| 36 | + // just ensure that the window is active. |
| 37 | + if (rootFrame == null) |
| 38 | + { |
| 39 | + // Create a Frame to act as the navigation context and navigate to the first page |
| 40 | + rootFrame = new Frame(); |
| 41 | + |
| 42 | + rootFrame.NavigationFailed += OnNavigationFailed; |
| 43 | + |
| 44 | + if (args.PreviousExecutionState == ApplicationExecutionState.Terminated) |
| 45 | + { |
| 46 | + // TODO: Load state from previously suspended application |
| 47 | + } |
| 48 | + |
| 49 | + // Place the frame in the current Window |
| 50 | + Window.Current.Content = rootFrame; |
| 51 | + } |
| 52 | + |
| 53 | + Microsoft.VisualStudio.TestPlatform.TestExecutor.UnitTestClient.CreateDefaultUI(); |
| 54 | + |
| 55 | + // Ensure the current window is active |
| 56 | + Window.Current.Activate(); |
| 57 | + |
| 58 | + Microsoft.VisualStudio.TestPlatform.TestExecutor.UnitTestClient.Run(args.Arguments); |
| 59 | + } |
| 60 | + |
| 61 | + /// <summary> |
| 62 | + /// Invoked when Navigation to a certain page fails. |
| 63 | + /// </summary> |
| 64 | + /// <param name="sender">The Frame which failed navigation.</param> |
| 65 | + /// <param name="e">Details about the navigation failure.</param> |
| 66 | + private void OnNavigationFailed(object sender, NavigationFailedEventArgs e) |
| 67 | + => throw new Exception($"Failed to load page '{e.SourcePageType.FullName}'."); |
| 68 | + |
| 69 | + /// <summary> |
| 70 | + /// Invoked when application execution is being suspended. Application state is saved |
| 71 | + /// without knowing whether the application will be terminated or resumed with the contents |
| 72 | + /// of memory still intact. |
| 73 | + /// </summary> |
| 74 | + /// <param name="sender">The source of the suspend request.</param> |
| 75 | + /// <param name="e">Details about the suspend request.</param> |
| 76 | + private void OnSuspending(object sender, SuspendingEventArgs e) |
| 77 | + { |
| 78 | + SuspendingDeferral deferral = e.SuspendingOperation.GetDeferral(); |
| 79 | + |
| 80 | + // TODO: Save application state and stop any background activity |
| 81 | + deferral.Complete(); |
| 82 | + } |
| 83 | +} |
0 commit comments