延長顯示啟動顯示畫面Display a splash screen for more time

重要 APIImportant APIs

您可以為 app 建立延長式啟動顯示畫面,讓啟動顯示畫面的顯示時間變長。Display a splash screen for more time by creating an extended splash screen for your app. 這個延長的畫面是模仿您應用程式啟動時所顯示的啟動顯示畫面,但是您可以自訂這個畫面。This extended screen imitates the splash screen shown when your app is launched, but can be customized. 不論您是想要顯示即時載入資訊,或只是想給 app 額外的時間來準備好初始 UI,都可以利用延長式啟動顯示畫面來定義啟動體驗。Whether you want to show real-time loading information or simply give your app extra time to prepare its initial UI, an extended splash screen lets you define the launch experience.

注意

本主題中的「擴充的啟動顯示畫面」一詞是指在一段長時間內停留在螢幕上的啟動顯示畫面。The phrase "extended splash screen" in this topic refers to a splash screen that stays on the screen for an extended period of time. 這不是指衍生自 SplashScreen 類別的子類別。It does not mean a subclass that derives from the SplashScreen class.

請遵循下列建議,以確定您的延長式啟動顯示畫面準確模仿預設啟動顯示畫面:Make sure your extended splash screen accurately imitates the default splash screen by following these recommendations:

  • 您的延長式啟動顯示畫面頁面應該使用 620 x 300 像素影像,此影像會與您應用程式資訊清單中針對啟動顯示畫面指定的影像一致 (您應用程式的啟動顯示畫面影像)。Your extended splash screen page should use a 620 x 300 pixel image that is consistent with the image specified for your splash screen in your app manifest (your app's splash screen image). 在 Microsoft Visual Studio 2015 中,啟動顯示畫面設定是儲存在應用程式資訊清單 (Package.appxmanifest 檔案) 之 [視覺資產] 索引標籤的 [啟動顯示畫面] 區段。In Microsoft Visual Studio 2015, splash screen settings are stored in the Splash Screen section of the Visual Assets tab in your app manifest (Package.appxmanifest file).
  • 您的延長式啟動顯示畫面所使用的背景色彩,應該與您應用程式資訊清單中針對啟動顯示畫面指定的背景色彩一致 (您應用程式的啟動顯示畫面背景)。Your extended splash screen should use a background color that is consistent with the background color specified for your splash screen in your app manifest (your app's splash screen background).
  • 您的程式碼應該使用 SplashScreen 類別,將應用程式的啟動顯示畫面影像,放置在與預設啟動顯示畫面相同的螢幕座標上。Your code should use the SplashScreen class to position your app's splash screen image at the same screen coordinates as the default splash screen.
  • 您的程式碼應該藉由使用 SplashScreen 類別,將項目重新放置在延長式啟動顯示畫面上,以回應視窗調整大小事件 (例如當螢幕旋轉或應用程式被移到螢幕上另一個應用程式旁邊時)。Your code should respond to window resize events (such as when the screen is rotated or your app is moved next to another app onscreen) by using the SplashScreen class to reposition items on your extended splash screen.

使用下列步驟建立延長式啟動顯示畫面,有效模擬預設啟動顯示畫面。Use the following steps to create an extended splash screen that effectively imitates the default splash screen.

[空白頁面] 項目新增到您的現有 appAdd a Blank Page item to your existing app

這個主題是假設您想要將延長式啟動顯示畫面新增到使用 C#、Visual Basic 或 C++ 的現有通用 Windows 平台 (UWP) app 專案。This topic assumes you want to add an extended splash screen to an existing Universal Windows Platform (UWP) app project using C#, Visual Basic, or C++.

  • 在 Visual Studio 中開啟應用程式。Open your app in Visual Studio.
  • 從功能表列按或開啟 [專案],然後按一下 [加入新項目]Press or open Project from the menu bar and click Add New Item. 將會出現 [加入新項目] 對話方塊。An Add New Item dialog box will appear.
  • 從這個對話方塊中,將新的 空白頁面 新增至您的應用程式。From this dialog box, add a new Blank Page to your app. 這個主題將延長式啟動顯示畫面頁面命名為 "ExtendedSplash"。This topic names the extended splash screen page "ExtendedSplash".

加入 空白頁面 專案會產生兩個檔案,一個用於標記 (ExtendedSplash) ,另一個用於程式碼 (ExtendedSplash.xaml.cs) 。Adding a Blank Page item generates two files, one for markup (ExtendedSplash.xaml) and another for code (ExtendedSplash.xaml.cs).

延長式啟動顯示畫面的基本 XAMLEssential XAML for an extended splash screen

請依照這些步驟,將影像與進度控制項新增到您的延長式啟動顯示畫面。Follow these steps to add an image and progress control to your extended splash screen.

在您的 ExtendedSplash.xaml 檔案中:In your ExtendedSplash.xaml file:

  • 變更預設 Grid 元素的 Background 屬性,使其符合您在應用程式資訊清單中針對應用程式啟動顯示畫面設定的背景色彩 (在 Package.appxmanifest 檔案的 [視覺資產] 區段)。Change the Background property of the default Grid element to match the background color you set for your app's splash screen in your app manifest (in the Visual Assets section of your Package.appxmanifest file). 預設啟動顯示畫面色彩是淺灰色 (hex 值 # 464646) 。The default splash screen color is a light gray (hex value #464646). 請注意,這個 Grid 元素是當您建立新 [空白頁面] 時預設提供的元素。Note that this Grid element is provided by default when you create a new Blank Page. 您不需要使用 Grid;它只是用來建置延長式啟動顯示畫面的一個便利基礎。You don't have to use a Grid; it's just a convenient base for building an extended splash screen.
  • Canvas 元素新增到 GridAdd a Canvas element to the Grid. 您將使用這個 Canvas 來放置您的延長式啟動顯示畫面影像。You'll use this Canvas to position your extended splash screen image.
  • Image 元素新增到 CanvasAdd an Image element to the Canvas. 在您的延長式啟動顯示畫面使用 600 x 320 像素影像,亦即與您為預設啟動顯示畫面選擇的影像相同。Use the same 600 x 320 pixel image for your extended splash screen that you chose for the default splash screen.
  • (選用) 新增進度控制項以向使用者顯示您的應用程式正在載入。(Optional) Add a progress control to show users that your app is loading. 本主題新增了 ProgressRing 來代替確定或不確定的 ProgressBarThis topic adds a ProgressRing, instead of a determinate or indeterminate ProgressBar.

下列範例示範包含這些新增專案和變更的 方格The following example demonstrates a Grid with these additions and changes.

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

注意

此範例會將 ProgressRing 的寬度設定為20圖元。This example sets the width of the ProgressRing to 20 pixels. 您可以將其寬度設定為適合您應用程式的值,不過,如果寬度小於 20 像素,控制項將無法顯示。You can manually set its width to a value that works for your app, however, the control will not render at widths of less than 20 pixels.

延長式啟動顯示畫面類別的基本程式碼Essential code for an extended splash screen class

每當視窗大小 (僅 Windows) 或方向變更時,您的延長式啟動顯示畫面都必須予以回應。Your extended splash screen needs to respond whenever the window size (Windows only) or orientation changes. 您使用的影像位置必須更新,如此才能讓您的延長式啟動顯示畫面不論視窗如何變更都能保持美觀。The position of the image you use must be updated so that your extended splash screen looks good no matter how the window changes.

使用這些步驟來定義方法,以便正確顯示您的延長式啟動顯示畫面。Use these steps to define methods to correctly display your extended splash screen.

  1. 新增必要的命名空間Add required namespaces

    您必須將下列命名空間新增至 ExtendedSplash.xaml.cs ,才能存取 SplashScreen 類別、 Rect 結構和 SizeChanged 事件。You'll need to add the following namespaces to ExtendedSplash.xaml.cs to access the SplashScreen class, the Rect struct, and the Window.SizeChanged events.

    using Windows.ApplicationModel.Activation;
    using Windows.Foundation;
    using Windows.UI.Core;
    
  2. 建立部分類別並宣告類別變數Create a partial class and declare class variables

    請在 ExtendedSplash.xaml.cs 中納入下列程式碼,以建立部分類別來代表延長式啟動顯示畫面。Include the following code in ExtendedSplash.xaml.cs to create a partial class to represent an extended splash screen.

    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
    }
    

    這些類別變數是透過數個方法來使用。These class variables are used by several methods. splashImageRect 變數會儲存系統顯示應用程式的啟動顯示畫面影像的座標。The splashImageRect variable stores the coordinates where the system displayed the splash screen image for the app. splash 變數會儲存 SplashScreen 物件,而 dismissed 變數會追蹤系統顯示的啟動顯示畫面是否已關閉。The splash variable stores a SplashScreen object, and the dismissed variable tracks whether or not the splash screen that is displayed by the system has been dismissed.

  3. 定義可正確放置影像的類別建構函式Define a constructor for your class that correctly positions the image

    下列程式碼為用來接聽視窗調整大小事件的延長式啟動顯示畫面類別定義了建構函式、將影像與 (選用) 進度控制項放置在延長式啟動顯示畫面、建立用於瀏覽的框架,以及呼叫非同步方法來還原已儲存的工作階段狀態。The following code defines a constructor for the extended splash screen class that listens for window resizing events, positions the image and (optional) progress control on the extended splash screen, creates a frame for navigation, and calls an asynchronous method to restore a saved session state.

    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();            
    }
    

    請確定在類別建構函式中登錄您的 Window.SizeChanged 處理常式 (範例中的 ExtendedSplash_OnResize),以便讓應用程式在延長式啟動顯示畫面中正確放置影像。Make sure to register your Window.SizeChanged handler (ExtendedSplash_OnResize in the example) in your class constructor so that your app positions the image correctly in your extended splash screen.

  4. 定義類別方法,以便在您的延長式啟動顯示畫面中放置影像Define a class method to position the image in your extended splash screen

    這個程式碼示範如何使用 splashImageRect 類別變數,將影像放置在延長式啟動顯示畫面頁面。This code demonstrates how to position the image on the extended splash screen page with the splashImageRect class variable.

    void PositionImage()
    {
        extendedSplashImage.SetValue(Canvas.LeftProperty, splashImageRect.X);
        extendedSplashImage.SetValue(Canvas.TopProperty, splashImageRect.Y);
        extendedSplashImage.Height = splashImageRect.Height;
        extendedSplashImage.Width = splashImageRect.Width;
    }
    
  5. (選用) 定義類別方法以將進度控制項放置在您的延長式啟動顯示畫面(Optional) Define a class method to position a progress control in your extended splash screen

    如果您選擇將 ProgressRing 新增到延長式啟動顯示畫面,請將它放置在啟動顯示畫面影像的相對位置。If you chose to add a ProgressRing to your extended splash screen, position it relative to the splash screen image. 請將下列程式碼新增到 ExtendedSplash.xaml.cs,以將 ProgressRing 置中放在影像下方 32 像素的位置。Add the following code to ExtendedSplash.xaml.cs to center the ProgressRing 32 pixels below the image.

    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. 在類別內部,定義 Dismissed 事件的處理常式Inside the class, define a handler for the Dismissed event

    在 ExtendedSplash.xaml.cs 中將 dismissed 類別變數設定為 true,以在 SplashScreen.Dismissed 事件發生時予以回應。In ExtendedSplash.xaml.cs, respond when the SplashScreen.Dismissed event occurs by setting the dismissed class variable to true. 如果您的應用程式有安裝程式作業,請將它們新增到這個事件處理常式。If your app has setup operations, add them to this event 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...
    }
    

    在應用程式安裝完成後,請離開延長式啟動顯示畫面。After app setup is complete, navigate away from your extended splash screen. 下列程式碼定義了一個叫做 DismissExtendedSplash 的方法,這個方法會瀏覽至您應用程式的 MainPage.xaml 檔案中定義的 MainPageThe following code defines a method called DismissExtendedSplash that navigates to the MainPage defined in your app's MainPage.xaml file.

    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. 在類別內部,定義 Window.SizeChanged 事件的處理常式Inside the class, define a handler for Window.SizeChanged events

    請準備您的延長式啟動顯示畫面,讓它在使用者調整視窗大小時重新放置元素。Prepare your extended splash screen to reposition its elements if a user resizes the window. 這個程式碼會藉由擷取新座標並重新放置影像,在 Window.SizeChanged 事件發生時予以回應。This code responds when a Window.SizeChanged event occurs by capturing the new coordinates and repositioning the image. 如果您已將進度控制項新增到延長式啟動顯示畫面,也請將它重新放置在這個事件處理常式內。If you added a progress control to your extended splash screen, reposition it inside this event handler as well.

    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();
        }
    }
    

    注意

     在您嘗試取得映射位置之前,請確定類別變數 (splash) 包含有效的 SplashScreen 物件,如範例所示。 Before you try to get the image location make sure the class variable (splash) contains a valid SplashScreen object, as shown in the example.

     

  8. (選擇性) 新增類別方法以還原已儲存的工作階段狀態(Optional) Add a class method to restore a saved session state

    您在步驟 4:修改啟動啟用處理常式中新增到 OnLaunched 方法的程式碼,會導致您的應用程式在啟動時顯示延長式啟動顯示畫面。The code you added to the OnLaunched method in Step 4: Modify the launch activation handler causes your app to display an extended splash screen when it launches. 若要在擴充的啟動顯示畫面類別中合併與應用程式啟動相關的所有方法,您可以考慮將方法新增至 ExtendedSplash.xaml.cs 檔案,以還原應用程式的狀態。To consolidate all methods related to app launch in your extended splash screen class, you could consider adding a method to your ExtendedSplash.xaml.cs file to restore the app's state.

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

    當您修改 App.xaml.cs 中的啟動啟用處理常式時,如果 app 之前的 ApplicationExecutionStateTerminated,則也需要將 loadstate 設定為 true。When you modify the launch activation handler in App.xaml.cs, you'll also set loadstate to true if the previous ApplicationExecutionState of your app was Terminated. 如果是這種情況,RestoreState 方法會將 app 還原到它之前的狀態。If so, the RestoreState method restores the app to its previous state. 如需 app 啟動、暫停及終止的概觀,請參閱 App 週期For an overview of app launch, suspension, and termination, see App lifecycle.

修改啟動啟用處理常式Modify the launch activation handler

啟動應用程式時,系統會將啟動顯示畫面資訊傳遞給應用程式的啟動啟用事件處理常式。When your app is launched, the system passes splash screen information to the app's launch activation event handler. 您可以使用這項資訊,將影像正確放置在延長式啟動顯示畫面頁面上。You can use this information to correctly position the image on your extended splash screen page. 您可以從傳遞給 app OnLaunched 處理常式的啟用事件引數中取得這項啟動顯示畫面資訊 (請參閱下列程式碼中的 args 變數)。You can get this splash screen information from the activation event arguments that are passed to your app's OnLaunched handler (see the args variable in the following code).

如果您尚未覆寫 app 的 OnLaunched 處理常式,請參閱 App 週期,以了解如何處理啟用事件。If you have not already overridden the OnLaunched handler for your app, see App lifecycle to learn how to handle activation events.

在 App.xaml.cs 中,新增下列程式碼來建立和顯示延長式啟動顯示畫面。In App.xaml.cs, add the following code to create and display an extended splash screen.

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();
}

完整程式碼Complete code

下列程式碼與先前步驟中所顯示的程式碼片段稍有不同。The following code slightly differs from the snippets shown in the previous steps.

  • ExtendedSplash.xaml 包含一個 DismissSplash 按鈕。ExtendedSplash.xaml includes a DismissSplash button. 按一下這個按鈕時,事件處理常式 DismissSplashButton_Click 會呼叫 DismissExtendedSplash 方法。When this button is clicked, an event handler, DismissSplashButton_Click, calls the DismissExtendedSplash method. 在您的應用程式中,請在應用程式完成資源載入或 UI 初始化時呼叫 DismissExtendedSplashIn your app, call DismissExtendedSplash when your app is done loading resources or initializing its UI.
  • 這個 app 也使用一個 UWP app 專案範本,而該範本使用 Frame 瀏覽。This app also uses a UWP app project template, which uses Frame navigation. 因此,在 App.xaml.cs 中,啟動啟用處理常式 (OnLaunched) 定義了 rootFrame,並使用它來設定應用程式視窗的內容。As a result, in App.xaml.cs, the launch activation handler (OnLaunched) defines a rootFrame and uses it to set the content of the app window.

ExtendedSplash .xamlExtendedSplash.xaml

此範例包含 DismissSplash 按鈕,因為它沒有要載入的應用程式資源。This example includes a DismissSplash button because it doesn't have app resources to load. 在您的應用程式中,請在應用程式完成資源載入或其初始 UI 準備時,自動關閉延長式啟動顯示畫面。In your app, dismiss the extended splash screen automatically when your app is done loading resources or preparing its initial UI.

<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.csExtendedSplash.xaml.cs

請注意, DismissExtendedSplash 會從按鈕的 click 事件處理常式呼叫方法 DismissSplashNote that the DismissExtendedSplash method is called from the click event handler for the DismissSplash button. 在您的應用程式中將不需要 DismissSplash 按鈕。In your app, you won't need a DismissSplash button. 請在應用程式完成資源載入,而您想要瀏覽至它的主頁面時,改為呼叫 DismissExtendedSplashInstead, call DismissExtendedSplash when your app is done loading resources and you want to navigate to its main page.

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.csApp.xaml.cs

此專案是在 Visual Studio 中使用 UWP 應用程式 **空白應用程式 (XAML) ** 專案範本所建立。This project was created using the UWP app Blank App (XAML) project template in Visual Studio. OnNavigationFailedOnSuspending 事件處理常式都是自動產生的,而且不需變更就可以實作延長式啟動顯示畫面。Both the OnNavigationFailed and OnSuspending event handlers are automatically generated and don't need to be changed to implement an extended splash screen. 這個主題只會修改 OnLaunchedThis topic only modifies OnLaunched.

如果您沒有將專案範本用於您的 app,請參閱步驟 4:修改啟動啟用處理常式,當中有一個沒有使用 Frame 瀏覽的已修改 OnLaunched 範例。If you didn't use a project template for your app, see Step 4: Modify the launch activation handler for an example of a modified OnLaunched that doesn't use Frame navigation.

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 applicaiton state and stop any background activity
            deferral.Complete();
        }
    }
}

參考Reference