App.xaml.cs (5828B)
1 // Copyright (c) Microsoft. All rights reserved. 2 3 using System; 4 using System.Threading.Tasks; 5 using Windows.ApplicationModel; 6 using Windows.ApplicationModel.Activation; 7 using Windows.UI.Xaml; 8 using Windows.UI.Xaml.Controls; 9 using Windows.UI.Xaml.Navigation; 10 using Windows.UI; 11 using File360.Common; 12 13 namespace File360 14 { 15 /// <summary> 16 /// Provides application-specific behavior to supplement the default Application class. 17 /// </summary> 18 sealed partial class App : Application 19 { 20 public string UserName = "User"; 21 #if WINDOWS_PHONE_APP 22 ContinuationManager continuationManager; 23 #endif 24 /// <summary> 25 /// Initializes the singleton Application object. This is the first line of authored code 26 /// executed, and as such is the logical equivalent of main() or WinMain(). 27 /// </summary> 28 public App() 29 { 30 this.InitializeComponent(); 31 this.Suspending += OnSuspending; 32 } 33 public Color AccentColor { set; get; } 34 public Color ThemeColor { set; get; } 35 public Color FolderColor { set; get; } 36 public ColorList CurrentColorItem { set; get; } 37 38 private Frame CreateRootFrame() 39 { 40 Frame rootFrame = Window.Current.Content as Frame; 41 42 // Do not repeat app initialization when the Window already has content, 43 // just ensure that the window is active 44 if (rootFrame == null) 45 { 46 // Create a Frame to act as the navigation context and navigate to the first page 47 rootFrame = new Frame(); 48 49 // Set the default language 50 rootFrame.Language = Windows.Globalization.ApplicationLanguages.Languages[0]; 51 rootFrame.NavigationFailed += OnNavigationFailed; 52 53 // Place the frame in the current Window 54 Window.Current.Content = rootFrame; 55 } 56 57 return rootFrame; 58 } 59 60 private async Task RestoreStatusAsync(ApplicationExecutionState previousExecutionState) 61 { 62 // Do not repeat app initialization when the Window already has content, 63 // just ensure that the window is active 64 if (previousExecutionState == ApplicationExecutionState.Terminated) 65 { 66 // Restore the saved session state only when appropriate 67 try 68 { 69 await SuspensionManager.RestoreAsync(); 70 } 71 catch (SuspensionManagerException) 72 { 73 //Something went wrong restoring state. 74 //Assume there is no state and continue 75 } 76 } 77 } 78 79 #if WINDOWS_PHONE_APP 80 /// <summary> 81 /// Handle OnActivated event to deal with File Open/Save continuation activation kinds 82 /// </summary> 83 /// <param name="e">Application activated event arguments, it can be casted to proper sub-type based on ActivationKind</param> 84 protected async override void OnActivated(IActivatedEventArgs e) 85 { 86 base.OnActivated(e); 87 88 continuationManager = new ContinuationManager(); 89 90 Frame rootFrame = CreateRootFrame(); 91 await RestoreStatusAsync(e.PreviousExecutionState); 92 93 if (rootFrame.Content == null) 94 { 95 rootFrame.Navigate(typeof(MainPage)); 96 } 97 98 var continuationEventArgs = e as IContinuationActivatedEventArgs; 99 if (continuationEventArgs != null) 100 { 101 if (rootFrame != null) 102 { 103 // Call ContinuationManager to handle continuation activation 104 continuationManager.Continue(continuationEventArgs, rootFrame); 105 } 106 } 107 108 Window.Current.Activate(); 109 } 110 #endif 111 112 /// <summary> 113 /// Invoked when the application is launched normally by the end user. Other entry points 114 /// will be used such as when the application is launched to open a specific file. 115 /// </summary> 116 /// <param name="e">Details about the launch request and process.</param> 117 protected async override void OnLaunched(LaunchActivatedEventArgs e) 118 { 119 Frame rootFrame = CreateRootFrame(); 120 await RestoreStatusAsync(e.PreviousExecutionState); 121 122 //MainPage is always in rootFrame so we don't have to worry about restoring the navigation state on resume 123 rootFrame.Navigate(typeof(MainPage), e.Arguments); 124 125 // Ensure the current window is active 126 Window.Current.Activate(); 127 } 128 129 /// <summary> 130 /// Invoked when Navigation to a certain page fails 131 /// </summary> 132 /// <param name="sender">The Frame which failed navigation</param> 133 /// <param name="e">Details about the navigation failure</param> 134 void OnNavigationFailed(object sender, NavigationFailedEventArgs e) 135 { 136 throw new Exception("Failed to load Page " + e.SourcePageType.FullName); 137 } 138 139 /// <summary> 140 /// Invoked when application execution is being suspended. Application state is saved 141 /// without knowing whether the application will be terminated or resumed with the contents 142 /// of memory still intact. 143 /// </summary> 144 /// <param name="sender">The source of the suspend request.</param> 145 /// <param name="e">Details about the suspend request.</param> 146 private async void OnSuspending(object sender, SuspendingEventArgs e) 147 { 148 var deferral = e.SuspendingOperation.GetDeferral(); 149 await SuspensionManager.SaveAsync(); 150 deferral.Complete(); 151 } 152 } 153 }