| 
 | 1 | +// Licensed to the .NET Foundation under one or more agreements.  | 
 | 2 | +// The .NET Foundation licenses this file to you under the MIT license.  | 
 | 3 | +// See the LICENSE file in the project root for more information.  | 
 | 4 | + | 
 | 5 | +using System;  | 
 | 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 UITests.App  | 
 | 13 | +{  | 
 | 14 | +    public sealed partial class App  | 
 | 15 | +    {  | 
 | 16 | +        public App()  | 
 | 17 | +        {  | 
 | 18 | +            this.InitializeComponent();  | 
 | 19 | +            this.Suspending += OnSuspending;  | 
 | 20 | +        }  | 
 | 21 | + | 
 | 22 | +        /// <summary>  | 
 | 23 | +        /// Invoked when the application is launched normally by the end user.  Other entry points  | 
 | 24 | +        /// will be used such as when the application is launched to open a specific file.  | 
 | 25 | +        /// </summary>  | 
 | 26 | +        /// <param name="e">Details about the launch request and process.</param>  | 
 | 27 | +        protected override void OnLaunched(LaunchActivatedEventArgs e)  | 
 | 28 | +        {  | 
 | 29 | +            Frame rootFrame = Window.Current.Content as Frame;  | 
 | 30 | + | 
 | 31 | +            // Do not repeat app initialization when the Window already has content,  | 
 | 32 | +            // just ensure that the window is active  | 
 | 33 | +            if (rootFrame == null)  | 
 | 34 | +            {  | 
 | 35 | +                // Create a Frame to act as the navigation context and navigate to the first page  | 
 | 36 | +                rootFrame = new Frame();  | 
 | 37 | + | 
 | 38 | +                rootFrame.NavigationFailed += OnNavigationFailed;  | 
 | 39 | + | 
 | 40 | +                if (e.PreviousExecutionState == ApplicationExecutionState.Terminated)  | 
 | 41 | +                {  | 
 | 42 | +                    // TODO: Load state from previously suspended application  | 
 | 43 | +                }  | 
 | 44 | + | 
 | 45 | +                // Place the frame in the current Window  | 
 | 46 | +                Window.Current.Content = rootFrame;  | 
 | 47 | +            }  | 
 | 48 | + | 
 | 49 | +            if (e.PrelaunchActivated == false)  | 
 | 50 | +            {  | 
 | 51 | +                if (rootFrame.Content == null)  | 
 | 52 | +                {  | 
 | 53 | +                    // When the navigation stack isn't restored navigate to the first page,  | 
 | 54 | +                    // configuring the new page by passing required information as a navigation  | 
 | 55 | +                    // parameter  | 
 | 56 | +                    rootFrame.Navigate(typeof(MainPage), e.Arguments);  | 
 | 57 | +                }  | 
 | 58 | + | 
 | 59 | +                // Ensure the current window is active  | 
 | 60 | +                Window.Current.Activate();  | 
 | 61 | +            }  | 
 | 62 | +        }  | 
 | 63 | + | 
 | 64 | +        /// <summary>  | 
 | 65 | +        /// Invoked when Navigation to a certain page fails  | 
 | 66 | +        /// </summary>  | 
 | 67 | +        /// <param name="sender">The Frame which failed navigation</param>  | 
 | 68 | +        /// <param name="e">Details about the navigation failure</param>  | 
 | 69 | +        private void OnNavigationFailed(object sender, NavigationFailedEventArgs e)  | 
 | 70 | +        {  | 
 | 71 | +            throw new Exception("Failed to load Page " + e.SourcePageType.FullName);  | 
 | 72 | +        }  | 
 | 73 | + | 
 | 74 | +        /// <summary>  | 
 | 75 | +        /// Invoked when application execution is being suspended.  Application state is saved  | 
 | 76 | +        /// without knowing whether the application will be terminated or resumed with the contents  | 
 | 77 | +        /// of memory still intact.  | 
 | 78 | +        /// </summary>  | 
 | 79 | +        /// <param name="sender">The source of the suspend request.</param>  | 
 | 80 | +        /// <param name="e">Details about the suspend request.</param>  | 
 | 81 | +        private void OnSuspending(object sender, SuspendingEventArgs e)  | 
 | 82 | +        {  | 
 | 83 | +            var deferral = e.SuspendingOperation.GetDeferral();  | 
 | 84 | + | 
 | 85 | +            // TODO: Save application state and stop any background activity  | 
 | 86 | +            deferral.Complete();  | 
 | 87 | +        }  | 
 | 88 | +    }  | 
 | 89 | +}  | 
0 commit comments