/// <summary> /// Invoked when application execution is being suspended. Application state is saved /// without knowing whether the application will be terminated or resumed with the contents /// of memory still intact. /// </summary> /// <param name="sender">The source of the suspend request.</param> /// <param name="e">Details about the suspend request.</param> private void OnSuspending(object sender, SuspendingEventArgs e) { var deferral = e.SuspendingOperation.GetDeferral(); //NotificationsExtensions.Toasts.ToastContent c = new NotificationsExtensions.Toasts.ToastContent() //{ // Visual = new NotificationsExtensions.Toasts.ToastVisual() // { // TitleText = new NotificationsExtensions.Toasts.ToastText() // { // Text = "Suspended" // } // } //}; //Windows.UI.Notifications.ToastNotificationManager.CreateToastNotifier().Show(new Windows.UI.Notifications.ToastNotification(c.GetXml())); //TODO: Save application state and stop any background activity // TODO: Cancel any current syncs // Register the tasks try { // Make sure none are registered (they should have already been unregistered) UnregisterAllBackgroundTasks(); RegisterInfrequentBackgroundTask(); RegisterRawPushBackgroundTask(); } catch (Exception ex) { if (UWPExceptionHelper.TrackIfRpcServerUnavailable(ex, "RegisterBgTasks")) { } else if (UWPExceptionHelper.TrackIfPathInvalid(ex, "RegisterBgTasks")) { } else { TelemetryExtension.Current?.TrackException(ex); } } deferral.Complete(); }
protected override async System.Threading.Tasks.Task OnLaunchedOrActivated(IActivatedEventArgs e) { try { #if DEBUG //if (System.Diagnostics.Debugger.IsAttached) //{ // this.DebugSettings.EnableFrameRateCounter = true; //} #endif // Register background tasks if (!_registeredBackgroundTasks) { try { // Make sure none are registered (they should have already been unregistered) UnregisterAllBackgroundTasks(); RegisterInfrequentBackgroundTask(); RegisterRawPushBackgroundTask(); RegisterToastBackgroundTask(); } catch (Exception ex) { if (UWPExceptionHelper.TrackIfRpcServerUnavailable(ex, "RegisterBgTasks")) { } else if (UWPExceptionHelper.TrackIfPathInvalid(ex, "RegisterBgTasks")) { } else { TelemetryExtension.Current?.TrackException(ex); } } _registeredBackgroundTasks = true; } // Wait for initialization to complete, to ensure we don't accidently add multiple windows // Although right now we don't even do any async tasks, so this will be useless await PowerPlannerApp.InitializeTask; MainAppWindow mainAppWindow; // If no windows, need to register window mainAppWindow = PowerPlannerApp.Current.Windows.OfType <MainAppWindow>().FirstOrDefault(); if (mainAppWindow == null) { // This configures the view models, does NOT call Activate yet var nativeWindow = new NativeUwpAppWindow(); mainAppWindow = new MainAppWindow(); await PowerPlannerApp.Current.RegisterWindowAsync(mainAppWindow, nativeWindow); if (PowerPlannerApp.Current.Windows.Count > 1) { throw new Exception("There are more than 1 windows registered"); } } if (e is LaunchActivatedEventArgs) { var launchEventArgs = e as LaunchActivatedEventArgs; var launchContext = !object.Equals(launchEventArgs.TileId, "App") ? LaunchSurface.SecondaryTile : LaunchSurface.Normal; if (launchContext == LaunchSurface.Normal) { // Track whether was launched from primary tile if (ApiInformation.IsPropertyPresent(typeof(LaunchActivatedEventArgs).FullName, nameof(LaunchActivatedEventArgs.TileActivatedInfo))) { if (launchEventArgs.TileActivatedInfo != null) { launchContext = LaunchSurface.PrimaryTile; } } } await HandleArguments(mainAppWindow, launchEventArgs.Arguments, launchContext); } else if (e is ToastNotificationActivatedEventArgs) { var args = e as ToastNotificationActivatedEventArgs; await HandleArguments(mainAppWindow, args.Argument, LaunchSurface.Toast); } else if (e is ProtocolActivatedEventArgs) { var protocolEventArgs = e as ProtocolActivatedEventArgs; if (!string.IsNullOrWhiteSpace(protocolEventArgs.Uri.PathAndQuery) && protocolEventArgs.Uri.PathAndQuery.StartsWith("?")) { await HandleArguments(mainAppWindow, protocolEventArgs.Uri.PathAndQuery.Substring(1), LaunchSurface.Uri); } } else if (e is AppointmentsProviderShowAppointmentDetailsActivatedEventArgs) { // Note that this code is essentially deprecated and doesn't get hit... Uri launch happens instead var showDetailsArgs = e as AppointmentsProviderShowAppointmentDetailsActivatedEventArgs; try { AppointmentsHelper.RoamingIdData data = AppointmentsHelper.RoamingIdData.FromString(showDetailsArgs.RoamingId); string finalArgs = null; switch (data.ItemType) { case ItemType.Schedule: finalArgs = new ViewScheduleArguments() { LocalAccountId = data.LocalAccountId }.SerializeToString(); break; case ItemType.MegaItem: finalArgs = new ViewTaskArguments() { LocalAccountId = data.LocalAccountId, ItemId = data.Identifier }.SerializeToString(); break; } if (finalArgs != null) { await HandleArguments(mainAppWindow, finalArgs, LaunchSurface.Calendar); } } catch (Exception ex) { TelemetryExtension.Current?.TrackException(ex); } } if (mainAppWindow.GetViewModel().Content == null) { await mainAppWindow.GetViewModel().HandleNormalLaunchActivation(); } Window.Current.Activate(); // Listen to window activation changes Window.Current.Activated += Current_Activated; // Set up the default window properties ConfigureWindowProperties(); // Set up the sharing support ConfigureDataTransferManager(); // Display updates HandleVersionChange(); } catch (Exception ex) { TelemetryExtension.Current?.TrackException(ex); } }