Delen via


Een splashscreen weergeven voor een langere tijd

Toon een splashscreen voor een langere tijd door een uitgebreid splashscreen voor uw app te maken. Dit uitgebreide scherm imiteert het welkomstscherm dat wordt weergegeven wanneer uw app wordt gestart, maar kan worden aangepast. Of u nu realtime laadgegevens wilt weergeven of uw app extra tijd wilt geven om de oorspronkelijke gebruikersinterface voor te bereiden, met een uitgebreid welkomstscherm kunt u de startervaring definiëren.

Opmerking

De zin 'uitgebreid welkomstscherm' in dit onderwerp verwijst naar een welkomstscherm dat gedurende langere tijd op het scherm blijft staan. Dit betekent niet een subklasse die is afgeleid van de SplashScreen-klasse.

Belangrijke API's

De volgende API's worden in dit onderwerp gebruikt:

Standaardaanbevelingen voor splashscreen

Zorg ervoor dat uw uitgebreide welkomstscherm het standaard welkomstscherm nauwkeurig imiteert door de volgende aanbevelingen te volgen:

  • Uw uitgebreide welkomstschermpagina moet een afbeelding van 620 x 300 pixels gebruiken die consistent is met de afbeelding die is opgegeven voor uw welkomstscherm in het app-manifest (de welkomstschermafbeelding van uw app). In Microsoft Visual Studio worden instellingen voor het welkomstscherm opgeslagen in de sectie Welkomstscherm van het tabblad Visual Assets in uw app-manifest (Package.appxmanifest-bestand).
  • Het uitgebreide welkomstscherm moet een achtergrondkleur gebruiken die consistent is met de achtergrondkleur die is opgegeven voor uw welkomstscherm in het app-manifest (de welkomstschermachtergrond van uw app).
  • Uw code moet de SplashScreen--klasse gebruiken om de welkomstschermafbeelding van uw app op dezelfde schermcoördinaten te plaatsen als het standaard welkomstscherm.
  • Uw code moet reageren op het wijzigen van het formaat van vensters (zoals wanneer het scherm wordt gedraaid of uw app wordt verplaatst naast een andere app op het scherm) met behulp van de SplashScreen-klasse om items op het uitgebreide welkomstscherm te verplaatsen.

Gebruik de volgende stappen om een uitgebreid welkomstscherm te maken dat het standaard welkomstscherm effectief imiteert.

Voeg een lege pagina-item toe aan uw bestaande app.

In dit onderwerp wordt ervan uitgegaan dat u een uitgebreid welkomstscherm wilt toevoegen aan een bestaand UWP-app-project (Universal Windows Platform) met C#, Visual Basic of C++.

  • Open uw app in Visual Studio.
  • Druk of open Project in de menubalk en klik op Nieuwe item toevoegen. Er wordt een dialoogvenster Nieuw item toevoegen weergegeven.
  • Voeg in dit dialoogvenster een nieuwe lege pagina toe aan uw app. In dit onderwerp wordt de uitgebreide welkomstschermpagina 'ExtendedSplash' genoemd.

Als u een lege pagina toevoegt item, worden twee bestanden gegenereerd, één voor markeringen (ExtendedSplash.xaml) en een andere voor code (ExtendedSplash.xaml.cs).

Essentiële XAML voor een uitgebreid welkomstscherm

Volg deze stappen om een afbeelding en een voortgangsbalk toe te voegen aan uw uitgebreide opstartscherm.

In het bestand ExtendedSplash.xaml:

  • Wijzig de eigenschap Achtergrond van het standaardelement Grid zodat deze overeenkomt met de achtergrondkleur die u hebt ingesteld voor het welkomstscherm van uw app in het app-manifest (in de sectie Visual Assets van het bestand Package.appxmanifest). De standaardkleur van het welkomstscherm is een lichtgrijs (hexwaarde #464646). Houd er rekening mee dat dit Grid-element standaard wordt geleverd wanneer u een nieuwe lege paginamaakt. U hoeft geen Grid-te gebruiken; het is slechts een handige basis voor het bouwen van een uitgebreid splashscreen.
  • Voeg een element Canvas toe aan het Raster. Je gebruikt deze Canvas- om je uitgebreide splashscreen-afbeelding te plaatsen.
  • Voeg een afbeeldingselement toe aan het canvas . Gebruik dezelfde afbeelding van 620 x 300 pixels voor uw uitgebreide welkomstscherm dat u hebt gekozen voor het standaard welkomstscherm.
  • (Optioneel) Voeg een voortgangsbeheer toe om gebruikers weer te geven dat uw app wordt geladen. In dit onderwerp wordt een ProgressRingtoegevoegd in plaats van een bepaalde of onbepaalde ProgressBar.

In het volgende voorbeeld ziet u een Grid- met deze toevoegingen en wijzigingen.

    <Grid Background="#464646">
        <Canvas>
            <Image x:Name="extendedSplashImage" Source="Assets/SplashScreen.png"/>
            <ProgressRing Name="splashProgressRing" IsActive="True" Width="20" HorizontalAlignment="Center"></ProgressRing>
        </Canvas>
    </Grid>

Opmerking

In dit voorbeeld wordt de breedte van de ProgressRing ingesteld op 20 pixels. U kunt de breedte handmatig instellen op een waarde die geschikt is voor uw app, maar het besturingselement wordt niet weergegeven met een breedte van minder dan 20 pixels.

Essentiële code voor een uitgebreide splashscreen-klasse

Uw uitgebreide welkomstscherm moet reageren wanneer de venstergrootte (alleen Windows) of de afdrukstand verandert. De positie van de afbeelding die u gebruikt, moet worden bijgewerkt, zodat uw uitgebreide welkomstscherm er goed uitziet, ongeacht hoe het venster verandert.

Gebruik deze stappen om methoden te definiëren om uw uitgebreide welkomstscherm correct weer te geven.

  1. vereiste naamruimten toevoegen

    U moet de volgende naamruimten toevoegen aan ExtendedSplash.xaml.cs om toegang te krijgen tot de SplashScreen-klasse, de Rect-struct en de Window.SizeChanged-gebeurtenissen.

    using Windows.ApplicationModel.Activation;
    using Windows.Foundation;
    using Windows.UI.Core;
    
  2. een gedeeltelijke klasse maken en klassevariabelen declareren

    Neem de volgende code op in ExtendedSplash.xaml.cs om een gedeeltelijke klasse te maken die een uitgebreid welkomstscherm vertegenwoordigt.

    partial class ExtendedSplash : Page
    {
        internal Rect splashImageRect; // Rect to store splash screen image coordinates.
        private SplashScreen splash; // Variable to hold the splash screen object.
        internal bool dismissed = false; // Variable to track splash screen dismissal status.
        internal Frame rootFrame;
    
       // Define methods and constructor
    }
    

    Deze klassevariabelen worden door verschillende methoden gebruikt. De variabele splashImageRect slaat de coördinaten op waar het systeem de welkomstschermafbeelding voor de app heeft weergegeven. De splash-variabele slaat een SplashScreen--object op en de dismissed variabele houdt bij of het welkomstscherm dat door het systeem wordt weergegeven, is gesloten.

  3. Definieer een constructor voor uw klasse die de afbeelding correct plaatst

    De volgende code definieert een constructor voor de uitgebreide welkomstschermklasse die luistert naar gebeurtenissen voor het wijzigen van het formaat van vensters, plaatst de afbeelding en (optioneel) voortgangsbeheer op het uitgebreide welkomstscherm, maakt een frame voor navigatie en roept een asynchrone methode aan om een opgeslagen sessiestatus te herstellen.

    public ExtendedSplash(SplashScreen splashscreen, bool loadState)
    {
        InitializeComponent();
    
        // Listen for window resize events to reposition the extended splash screen image accordingly.
        // This ensures that the extended splash screen formats properly in response to window resizing.
        Window.Current.SizeChanged += new WindowSizeChangedEventHandler(ExtendedSplash_OnResize);
    
        splash = splashscreen;
        if (splash != null)
        {
            // Register an event handler to be executed when the splash screen has been dismissed.
            splash.Dismissed += new TypedEventHandler<SplashScreen, Object>(DismissedEventHandler);
    
            // Retrieve the window coordinates of the splash screen image.
            splashImageRect = splash.ImageLocation;
            PositionImage();
    
            // If applicable, include a method for positioning a progress control.
            PositionRing();
        }
    
        // Create a Frame to act as the navigation context
        rootFrame = new Frame();            
    }
    

    Zorg ervoor dat u uw Window.SizeChanged handler (ExtendedSplash_OnResize in het voorbeeld) registreert in uw klasseconstructor, zodat de app de afbeelding correct in het uitgebreide welkomstscherm plaatst.

  4. Definieer een klassemethode om de afbeelding in uw uitgebreide welkomstscherm te plaatsen

    Deze code laat zien hoe u de afbeelding op de uitgebreide welkomstschermpagina kunt plaatsen met de variabele splashImageRect klasse.

    void PositionImage()
    {
        extendedSplashImage.SetValue(Canvas.LeftProperty, splashImageRect.X);
        extendedSplashImage.SetValue(Canvas.TopProperty, splashImageRect.Y);
        extendedSplashImage.Height = splashImageRect.Height;
        extendedSplashImage.Width = splashImageRect.Width;
    }
    
  5. (Optioneel) Definieer een klassemethode om een voortgangscontrole in uw uitgebreide splashscreen te plaatsen

    Als u ervoor kiest om een ProgressRing- toe te voegen aan uw uitgebreide welkomstscherm, plaatst u deze ten opzichte van de welkomstschermafbeelding. Voeg de volgende code toe aan ExtendedSplash.xaml.cs om de ProgressRing 32 pixels onder de afbeelding te centreren.

    void PositionRing()
    {
        splashProgressRing.SetValue(Canvas.LeftProperty, splashImageRect.X + (splashImageRect.Width*0.5) - (splashProgressRing.Width*0.5));
        splashProgressRing.SetValue(Canvas.TopProperty, (splashImageRect.Y + splashImageRect.Height + splashImageRect.Height*0.1));
    }
    
  6. Binnen de klasse definieer een handler voor het afgewezen evenement

    Reageer in ExtendedSplash.xaml.cs wanneer het evenement SplashScreen.Dismissed plaatsvindt door de klassevariabele dismissed op true in te stellen. Als uw app installatiebewerkingen heeft, voegt u deze toe aan deze gebeurtenis-handler.

    // Include code to be executed when the system has transitioned from the splash screen to the extended splash screen (application's first view).
    void DismissedEventHandler(SplashScreen sender, object e)
    {
        dismissed = true;
    
        // Complete app setup operations here...
    }
    

    Nadat de installatie van de app is voltooid, navigeert u weg van uw uitgebreide welkomstscherm. De volgende code definieert een methode met de naam DismissExtendedSplash die naar de MainPage navigeert die is gedefinieerd in het Bestand MainPage.xaml van uw app.

    async void DismissExtendedSplash()
      {
         await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,() =>            {
              rootFrame = new Frame();
              rootFrame.Content = new MainPage(); Window.Current.Content = rootFrame;
            });
      }
    
  7. Binnen de klasse definieert u een handler voor Window.SizeChanged-gebeurtenissen

    Bereid uw uitgebreide welkomstscherm voor om de elementen ervan te verplaatsen als een gebruiker het formaat van het venster wijzigt. Deze code reageert wanneer een Window.SizeChanged gebeurtenis plaatsvindt door de nieuwe coördinaten vast te leggen en de afbeelding te verplaatsen. Als u een voortgangsbalk hebt toegevoegd aan uw uitgebreide welkomstscherm, verplaatst u deze ook binnen deze gebeurtenishandler.

    void ExtendedSplash_OnResize(Object sender, WindowSizeChangedEventArgs e)
    {
        // Safely update the extended splash screen image coordinates. This function will be executed when a user resizes the window.
        if (splash != null)
        {
            // Update the coordinates of the splash screen image.
            splashImageRect = splash.ImageLocation;
            PositionImage();
    
            // If applicable, include a method for positioning a progress control.
            // PositionRing();
        }
    }
    

    Opmerking

     Voordat u de afbeeldingslocatie probeert op te halen, moet u ervoor zorgen dat de klassevariabele (splash) een geldig SplashScreen--object bevat, zoals wordt weergegeven in het voorbeeld.

     

  8. (optioneel) Een klassemethode toevoegen om een opgeslagen sessiestatus te herstellen

    De code die u hebt toegevoegd aan de methode OnLaunched in stap 4: De startactiveringshandler wijzigen zorgt ervoor dat uw app een uitgebreid opstartscherm weergeeft wanneer deze wordt gestart. Als u alle methoden met betrekking tot het starten van apps in uw uitgebreide welkomstschermklasse wilt samenvoegen, kunt u overwegen een methode toe te voegen aan uw ExtendedSplash.xaml.cs-bestand om de status van de app te herstellen.

    void RestoreState(bool loadState)
    {
        if (loadState)
        {
             // code to load your app's state here
        }
    }
    

    Wanneer u de startactiveringshandler in App.xaml.cs wijzigt, stelt u ook loadstate in op true als de vorige ApplicationExecutionState- van uw app beëindigdwas. Zo ja, herstelt de RestoreState-methode de app naar zijn vorige toestand. Zie app-levenscyclusvoor een overzicht van het starten, opschorten en beëindigen van apps.

De startactiveringshandler wijzigen

Wanneer uw app wordt gestart, geeft het systeem informatie over het splash screen door aan de startgebeurtenis-handler van de app. U kunt deze informatie gebruiken om de afbeelding correct te plaatsen op uw uitgebreide welkomstschermpagina. U kunt deze splashscreen-informatie ophalen uit de activeringsgebeurtenisargumenten die worden doorgegeven aan de OnLaunched-handler (zie de args-variabele in de volgende code).

Als u de OnLaunched handler voor uw app nog niet hebt overschreven, raadpleegt u levenscyclus van apps voor meer informatie over het afhandelen van activeringsgebeurtenissen.

Voeg in App.xaml.cs de volgende code toe om een uitgebreid welkomstscherm te maken en weer te geven.

protected override void OnLaunched(LaunchActivatedEventArgs args)
{
    if (args.PreviousExecutionState != ApplicationExecutionState.Running)
    {
        bool loadState = (args.PreviousExecutionState == ApplicationExecutionState.Terminated);
        ExtendedSplash extendedSplash = new ExtendedSplash(args.SplashScreen, loadState);
        Window.Current.Content = extendedSplash;
    }

    Window.Current.Activate();
}

De volledige code

De volgende code verschilt enigszins van de fragmenten die in de vorige stappen worden weergegeven.

  • ExtendedSplash.xaml bevat een DismissSplash knop. Wanneer op deze knop wordt geklikt, roept een gebeurtenis-handler, DismissSplashButton_Click, de methode DismissExtendedSplash aan. Roep in uw app DismissExtendedSplash aan wanneer uw app klaar is met het laden van resources of het initialiseren van de gebruikersinterface.
  • Deze app maakt ook gebruik van een UWP-app-projectsjabloon, die gebruikmaakt van Frame navigatie. Als gevolg hiervan definieert de startactiveringshandler (OnLaunched) in App.xaml.cs een rootFrame en gebruikt deze om de inhoud van het app-venster in te stellen.

ExtendedSplash.xaml

Dit voorbeeld bevat een DismissSplash knop omdat er geen app-resources zijn om te laden. Sluit in uw app het uitgebreide welkomstscherm automatisch wanneer uw app klaar is met het laden van resources of het voorbereiden van de oorspronkelijke gebruikersinterface.

<Page
    x:Class="SplashScreenExample.ExtendedSplash"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:SplashScreenExample"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <Grid Background="#464646">
        <Canvas>
            <Image x:Name="extendedSplashImage" Source="Assets/SplashScreen.png"/>
            <ProgressRing Name="splashProgressRing" IsActive="True" Width="20" HorizontalAlignment="Center"/>
        </Canvas>
        <StackPanel HorizontalAlignment="Center" VerticalAlignment="Bottom">
            <Button x:Name="DismissSplash" Content="Dismiss extended splash screen" HorizontalAlignment="Center" Click="DismissSplashButton_Click" />
        </StackPanel>
    </Grid>
</Page>

ExtendedSplash.xaml.cs

Houd er rekening mee dat de methode DismissExtendedSplash wordt aangeroepen vanuit de klik-gebeurtenis-handler voor de knop DismissSplash. In uw app hebt u geen DismissSplash knop nodig. Roep in plaats daarvan DismissExtendedSplash aan wanneer uw app klaar is met het laden van resources en u naar de hoofdpagina wilt navigeren.

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

using Windows.ApplicationModel.Activation;
using SplashScreenExample.Common;
using Windows.UI.Core;

// The Blank Page item template is documented at https://go.microsoft.com/fwlink/p/?LinkID=234238

namespace SplashScreenExample
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    partial class ExtendedSplash : Page
    {
        internal Rect splashImageRect; // Rect to store splash screen image coordinates.
        private SplashScreen splash; // Variable to hold the splash screen object.
        internal bool dismissed = false; // Variable to track splash screen dismissal status.
        internal Frame rootFrame;

        public ExtendedSplash(SplashScreen splashscreen, bool loadState)
        {
            InitializeComponent();

            // Listen for window resize events to reposition the extended splash screen image accordingly.
            // This is important to ensure that the extended splash screen is formatted properly in response to snapping, unsnapping, rotation, etc...
            Window.Current.SizeChanged += new WindowSizeChangedEventHandler(ExtendedSplash_OnResize);

            splash = splashscreen;

            if (splash != null)
            {
                // Register an event handler to be executed when the splash screen has been dismissed.
                splash.Dismissed += new TypedEventHandler<SplashScreen, Object>(DismissedEventHandler);

                // Retrieve the window coordinates of the splash screen image.
                splashImageRect = splash.ImageLocation;
                PositionImage();

                // Optional: Add a progress ring to your splash screen to show users that content is loading
                PositionRing();
            }

            // Create a Frame to act as the navigation context
            rootFrame = new Frame();

            // Restore the saved session state if necessary
            RestoreState(loadState);
        }

        void RestoreState(bool loadState)
        {
            if (loadState)
            {
                // TODO: write code to load state
            }
        }

        // Position the extended splash screen image in the same location as the system splash screen image.
        void PositionImage()
        {
            extendedSplashImage.SetValue(Canvas.LeftProperty, splashImageRect.X);
            extendedSplashImage.SetValue(Canvas.TopProperty, splashImageRect.Y);
            extendedSplashImage.Height = splashImageRect.Height;
            extendedSplashImage.Width = splashImageRect.Width;

        }

        void PositionRing()
        {
            splashProgressRing.SetValue(Canvas.LeftProperty, splashImageRect.X + (splashImageRect.Width*0.5) - (splashProgressRing.Width*0.5));
            splashProgressRing.SetValue(Canvas.TopProperty, (splashImageRect.Y + splashImageRect.Height + splashImageRect.Height*0.1));
        }

        void ExtendedSplash_OnResize(Object sender, WindowSizeChangedEventArgs e)
        {
            // Safely update the extended splash screen image coordinates. This function will be fired in response to snapping, unsnapping, rotation, etc...
            if (splash != null)
            {
                // Update the coordinates of the splash screen image.
                splashImageRect = splash.ImageLocation;
                PositionImage();
                PositionRing();
            }
        }

        // Include code to be executed when the system has transitioned from the splash screen to the extended splash screen (application's first view).
        void DismissedEventHandler(SplashScreen sender, object e)
        {
            dismissed = true;

            // Complete app setup operations here...
        }

        void DismissExtendedSplash()
        {
            // Navigate to mainpage
            rootFrame.Navigate(typeof(MainPage));
            // Place the frame in the current Window
            Window.Current.Content = rootFrame;
        }

        void DismissSplashButton_Click(object sender, RoutedEventArgs e)
        {
            DismissExtendedSplash();
        }
    }
}

App.xaml.cs

Dit project is gemaakt met behulp van de UWP-app Blank App (XAML) projectsjabloon in Visual Studio. Zowel de OnNavigationFailed als OnSuspending gebeurtenishandlers worden automatisch gegenereerd en hoeven niet te worden gewijzigd om een uitgebreid opstartscherm te implementeren. In dit onderwerp wordt alleen OnLaunchedgewijzigd.

Als u geen projectsjabloon voor uw app hebt gebruikt, raadpleegt u stap 4: De startactiveringshandler wijzigen voor een voorbeeld van een gewijzigde OnLaunched die geen gebruik maakt van Frame navigatie.

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.ApplicationModel;
using Windows.ApplicationModel.Activation;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// The Blank Application template is documented at https://go.microsoft.com/fwlink/p/?LinkID=234227

namespace SplashScreenExample
{
    /// <summary>
    /// Provides application-specific behavior to supplement the default Application class.
    /// </summary>
    sealed partial class App : Application
    {
        /// <summary>
        /// Initializes the singleton application object.  This is the first line of authored code
        /// executed, and as such is the logical equivalent of main() or WinMain().
        /// </summary>
        public App()
        {
            Microsoft.ApplicationInsights.WindowsAppInitializer.InitializeAsync(
            Microsoft.ApplicationInsights.WindowsCollectors.Metadata |
            Microsoft.ApplicationInsights.WindowsCollectors.Session);
            this.InitializeComponent();
            this.Suspending += OnSuspending;
        }

        /// <summary>
        /// Invoked when the application is launched normally by the end user.  Other entry points
        /// will be used such as when the application is launched to open a specific file.
        /// </summary>
        /// <param name="e">Details about the launch request and process.</param>
        protected override void OnLaunched(LaunchActivatedEventArgs e)
        {
#if DEBUG
            if (System.Diagnostics.Debugger.IsAttached)
            {
                this.DebugSettings.EnableFrameRateCounter = true;
            }
#endif

            Frame rootFrame = Window.Current.Content as Frame;

            // Do not repeat app initialization when the Window already has content,
            // just ensure that the window is active
            if (rootFrame == null)
            {
                // Create a Frame to act as the navigation context and navigate to the first page
                rootFrame = new Frame();
                // Set the default language
                rootFrame.Language = Windows.Globalization.ApplicationLanguages.Languages[0];

                rootFrame.NavigationFailed += OnNavigationFailed;

                //  Display an extended splash screen if app was not previously running.
                if (e.PreviousExecutionState != ApplicationExecutionState.Running)
                {
                    bool loadState = (e.PreviousExecutionState == ApplicationExecutionState.Terminated);
                    ExtendedSplash extendedSplash = new ExtendedSplash(e.SplashScreen, loadState);
                    rootFrame.Content = extendedSplash;
                    Window.Current.Content = rootFrame;
                }
            }

            if (rootFrame.Content == null)
            {
                // When the navigation stack isn't restored navigate to the first page,
                // configuring the new page by passing required information as a navigation
                // parameter
                rootFrame.Navigate(typeof(MainPage), e.Arguments);
            }
            // Ensure the current window is active
            Window.Current.Activate();
        }

        /// <summary>
        /// Invoked when Navigation to a certain page fails
        /// </summary>
        /// <param name="sender">The Frame which failed navigation</param>
        /// <param name="e">Details about the navigation failure</param>
        void OnNavigationFailed(object sender, NavigationFailedEventArgs e)
        {
            throw new Exception("Failed to load Page " + e.SourcePageType.FullName);
        }

        /// <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();
            // TODO: Save application state and stop any background activity
            deferral.Complete();
        }
    }
}

Verwijzing