Catatan
Akses ke halaman ini memerlukan otorisasi. Anda dapat mencoba masuk atau mengubah direktori.
Akses ke halaman ini memerlukan otorisasi. Anda dapat mencoba mengubah direktori.
Tampilkan layar splash untuk lebih banyak waktu dengan membuat layar splash yang diperluas untuk aplikasi Anda. Layar yang diperluas ini meniru layar splash yang ditampilkan saat aplikasi Anda diluncurkan, tetapi dapat disesuaikan. Apakah Anda ingin menampilkan informasi pemuatan real time atau hanya memberi aplikasi Anda waktu tambahan untuk menyiapkan UI awalnya, layar splash yang diperluas memungkinkan Anda menentukan pengalaman peluncuran.
Nota
Frasa "layar percikan yang diperluas" dalam topik ini mengacu pada layar percikan yang tetap berada di layar untuk jangka waktu yang lama. Ini tidak berarti subkelas yang berasal dari kelas SplashScreen.
API penting
API berikut digunakan dalam topik ini:
- kelas
SplashScreen - Peristiwa Window.SizeChanged
- metode Application.OnLaunched
Rekomendasi layar pembuka default
Pastikan layar splash yang diperluas secara akurat meniru layar splash bawaan dengan mengikuti rekomendasi berikut:
- Halaman layar splash yang diperluas harus menggunakan gambar 620 x 300 piksel yang konsisten dengan gambar yang ditentukan untuk layar splash anda di manifes aplikasi (gambar layar splash aplikasi Anda). Di Microsoft Visual Studio, pengaturan layar splash disimpan di bagian Splash Screen dari tab Aset Visual di manifes aplikasi Anda (file Package.appxmanifest).
- Layar splash yang diperluas harus menggunakan warna latar belakang yang konsisten dengan warna latar belakang yang ditentukan untuk layar splash Anda di manifes aplikasi Anda (latar belakang layar splash aplikasi Anda).
- Kode Anda harus menggunakan kelas SplashScreen untuk memposisikan gambar layar splash aplikasi Anda pada koordinat yang sama dengan layar splash bawaan.
- Kode Anda harus merespons peristiwa pengubahan ukuran jendela (seperti saat layar diputar atau aplikasi Anda dipindahkan di samping aplikasi lain di layar) dengan menggunakan kelas SplashScreen untuk memposisikan ulang item di layar splash yang diperluas.
Gunakan langkah-langkah berikut untuk membuat layar percikan yang diperluas yang secara efektif meniru layar percikan default.
Tambahkan item Halaman Kosong ke aplikasi yang sudah ada
Topik ini mengasumsikan Anda ingin menambahkan layar percikan yang diperluas ke proyek aplikasi Universal Windows Platform (UWP) yang ada menggunakan C#, Visual Basic, atau C++.
- Buka aplikasi Anda di Visual Studio.
- Tekan atau buka Proyek dari bilah menu dan klik Tambahkan Item Baru. Kotak dialog Tambahkan Item Baru akan muncul.
- Dari kotak dialog ini, tambahkan Blank Page baru ke aplikasi Anda. Dokumen ini menamai halaman layar pembuka yang diperluas "ExtendedSplash".
Menambahkan item Halaman Kosong menghasilkan dua file, satu untuk markup (ExtendedSplash.xaml) dan satu lagi untuk kode (ExtendedSplash.xaml.cs).
XAML penting untuk layar pengantar yang diperluas
Ikuti langkah-langkah ini untuk menambahkan gambar dan kontrol kemajuan ke layar splash yang diperluas.
Dalam file ExtendedSplash.xaml Anda:
- Ubah properti Latar Belakang dari elemen Kisi default agar sesuai dengan warna latar belakang layar mula-mula aplikasi Anda yang ditetapkan di manifes aplikasi Anda (dalam bagian Aset Visual dari file Package.appxmanifest Anda). Warna layar percikan default adalah abu-abu muda (nilai hex #464646). Perhatikan bahwa elemen Kisi ini disediakan secara default saat Anda membuat Halaman Kosong baru . Anda tidak perlu menggunakan Grid; ini hanyalah basis yang memudahkan untuk membuat layar pembuka yang diperluas.
- Tambahkan elemen Canvas ke Grid . Anda akan menggunakan Canvas ini untuk menempatkan gambar layar splash yang diperluas.
- Tambahkan elemen Gambar
ke Canvas . Gunakan gambar 620 x 300 piksel yang sama untuk layar splash yang diperluas yang Anda pilih untuk layar splash default. - (Opsional) Tambahkan kontrol kemajuan untuk menampilkan kepada pengguna bahwa aplikasi Anda sedang dimuat. Topik ini menambahkan ProgressRing, sebagai gantinya daripada ProgressBaryang determinasi atau tidak determinasi.
Contoh berikut menunjukkan Grid dengan penambahan dan perubahan ini.
<Grid Background="#464646">
<Canvas>
<Image x:Name="extendedSplashImage" Source="Assets/SplashScreen.png"/>
<ProgressRing Name="splashProgressRing" IsActive="True" Width="20" HorizontalAlignment="Center"></ProgressRing>
</Canvas>
</Grid>
Nota
Contoh ini mengatur lebar ProgressRing menjadi 20 piksel. Anda dapat mengatur lebarnya secara manual ke nilai yang berfungsi untuk aplikasi Anda, namun, kontrol tidak akan merender dengan lebar kurang dari 20 piksel.
Kode penting untuk kelas layar pembuka yang diperluas
Layar pembuka yang diperluas perlu merespons setiap kali ukuran jendela (hanya Windows) atau orientasi berubah. Posisi gambar yang Anda gunakan harus diperbarui sehingga layar splash yang diperluas terlihat bagus tidak peduli bagaimana jendela berubah.
Gunakan langkah-langkah ini untuk menentukan metode yang tepat dalam menampilkan layar splash yang diperluas.
Menambahkan namespace yang diperlukan
Anda harus menambahkan namespace berikut ke ExtendedSplash.xaml.cs untuk mengakses SplashScreen class, struct Rect, dan Window.SizeChanged events.
using Windows.ApplicationModel.Activation; using Windows.Foundation; using Windows.UI.Core;
Membuat kelas parsial dan mendeklarasikan variabel kelas
Sertakan kode berikut dalam ExtendedSplash.xaml.cs untuk membuat kelas parsial untuk mewakili layar percikan yang diperluas.
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 }
Variabel kelas ini digunakan oleh beberapa metode. Variabel
splashImageRect
menyimpan koordinat tempat sistem menampilkan gambar layar percikan untuk aplikasi. Variabelsplash
menyimpan objek SplashScreen , dan variabeldismissed
melacak apakah splash screen yang ditampilkan oleh sistem sudah ditutup atau belum.Tentukan konstruktor untuk kelas Anda yang memposisikan gambar dengan benar
Kode berikut ini mendefinisikan konstruktor untuk kelas layar splash yang diperluas yang mendengarkan peristiwa mengubah ukuran jendela, memposisikan gambar dan kontrol kemajuan (opsional) pada layar splash yang diperluas, membuat bingkai navigasi, dan memanggil metode asinkron untuk memulihkan status sesi yang disimpan.
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(); }
Pastikan untuk mendaftarkan Window.SizeChanged handler (seperti yang ditunjukkan pada
ExtendedSplash_OnResize
dalam contoh) di konstruktor kelas Anda agar aplikasi Anda dapat memposisikan gambar dengan tepat di layar splash yang diperluas.Definisikan metode kelas untuk memosisikan gambar di layar pembuka yang diperluas
Kode ini menunjukkan cara memosisikan gambar pada halaman layar splash yang diperluas dengan
splashImageRect
variabel kelas.void PositionImage() { extendedSplashImage.SetValue(Canvas.LeftProperty, splashImageRect.X); extendedSplashImage.SetValue(Canvas.TopProperty, splashImageRect.Y); extendedSplashImage.Height = splashImageRect.Height; extendedSplashImage.Width = splashImageRect.Width; }
(Opsional) Tentukan metode kelas untuk memosisikan kontrol progres di layar splash yang telah diperluas
Jika Anda memilih untuk menambahkan
ProgressRing ke layar splash yang diperluas, posisikan relatif terhadap gambar layar splash. Tambahkan kode berikut ke ExtendedSplash.xaml.cs untuk memusatkan ProgressRing 32 piksel di bawah gambar. 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)); }
Di dalam kelas, definisikan handler untuk acara Dismissed
Dalam ExtendedSplash.xaml.cs, respons saat peristiwa SplashScreen.Dismissed terjadi dengan mengatur
dismissed
variabel kelas ke true. Jika aplikasi Anda memiliki operasi penyiapan, tambahkan ke penanganan aktivitas ini.// 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... }
Setelah penyiapan aplikasi selesai, navigasikan menjauh dari layar splash yang diperluas. Kode berikut mendefinisikan metode yang disebut
DismissExtendedSplash
yang menavigasi keMainPage
yang ditentukan dalam file MainPage.xaml aplikasi Anda.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; }); }
Di dalam kelas, tetapkan sebuah handler untuk menangani peristiwa Window.SizeChanged
Siapkan layar splash yang diperluas untuk memposisikan ulang elemennya jika pengguna mengubah ukuran jendela. Kode ini merespons ketika peristiwa Window.SizeChanged terjadi dengan menangkap koordinat baru dan memposisikan ulang gambar. Jika Anda menambahkan kontrol kemajuan ke layar percikan yang diperluas, reposisikan di dalam penanganan aktivitas ini juga.
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(); } }
Nota
Sebelum Anda mencoba mendapatkan lokasi gambar, pastikan variabel kelas (
splash
) berisi objek SplashScreen yang valid, seperti yang ditunjukkan dalam contoh.(Opsional) Menambahkan metode kelas untuk memulihkan status sesi yang disimpan
Kode yang Anda tambahkan ke metode OnLaunched di Langkah 4: Mengubah pengelola aktivasi peluncuran menyebabkan aplikasi Anda menampilkan layar splash yang diperluas saat diluncurkan. Untuk mengonsolidasikan semua metode yang terkait dengan peluncuran aplikasi di kelas layar splash yang diperluas, Anda dapat mempertimbangkan untuk menambahkan metode ke file ExtendedSplash.xaml.cs Anda untuk memulihkan status aplikasi.
void RestoreState(bool loadState) { if (loadState) { // code to load your app's state here } }
Saat mengubah handler aktivasi peluncuran di App.xaml.cs, Anda juga akan mengatur
ke true jika ApplicationExecutionState sebelumnya dari aplikasi Anda Dihentikan . Jika demikian,RestoreState
metode memulihkan aplikasi ke status sebelumnya. Untuk gambaran umum peluncuran, penangguhan, dan penghentian aplikasi, lihat Siklus hidup aplikasi.
Mengubah handler aktivasi peluncuran
Saat aplikasi Anda diluncurkan, sistem meneruskan informasi layar percikan ke penanganan aktivitas aktivasi peluncuran aplikasi. Anda dapat menggunakan informasi ini untuk memposisikan gambar dengan benar di halaman layar pembuka yang diperluas. Anda bisa mendapatkan informasi layar splash ini dari argumen peristiwa aktivasi yang diteruskan ke handler OnLaunched aplikasi Anda (lihat args
variabel dalam kode berikut).
Jika Anda belum mengambil alih handler OnLaunched untuk aplikasi Anda, lihat siklus hidup aplikasi untuk mempelajari cara mengelola peristiwa aktivasi.
Di App.xaml.cs, tambahkan kode berikut untuk membuat dan menampilkan layar splash yang diperluas.
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();
}
Kode lengkap
Kode berikut sedikit berbeda dari cuplikan yang ditunjukkan pada langkah-langkah sebelumnya.
- ExtendedSplash.xaml menyertakan
DismissSplash
tombol. Ketika tombol ini diklik, pengendali kejadian,DismissSplashButton_Click
, memanggil metodeDismissExtendedSplash
. Di aplikasi Anda, panggilDismissExtendedSplash
saat aplikasi Anda selesai memuat sumber daya atau menginisialisasi UI-nya. - Aplikasi ini juga menggunakan templat proyek aplikasi UWP, yang menggunakan navigasi Bingkai . Akibatnya, dalam App.xaml.cs, handler aktivasi peluncuran (OnLaunched) mendefinisikan
rootFrame
dan menggunakannya untuk mengatur konten jendela aplikasi.
ExtendedSplash.xaml
Contoh ini mencakup tombol DismissSplash
karena tidak memiliki sumber daya aplikasi untuk dimuat. Di aplikasi Anda, tutup layar splash yang diperluas secara otomatis saat aplikasi Anda selesai memuat sumber daya atau menyiapkan antarmuka pengguna awalnya.
<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
Perhatikan bahwa metode DismissExtendedSplash
dipanggil dari penangan penanganan peristiwa klik untuk tombol DismissSplash
. Di aplikasi, Anda tidak memerlukan DismissSplash
tombol. Sebagai gantinya, hubungi DismissExtendedSplash
saat aplikasi Anda selesai memuat sumber daya dan Anda ingin menavigasi ke halaman utamanya.
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
Proyek ini dibuat menggunakan templat proyek Aplikasi UWP Kosong (XAML) di Visual Studio. Penanganan aktivitas OnNavigationFailed
dan OnSuspending
dibuat secara otomatis dan tidak perlu diubah untuk mengimplementasikan layar percikan yang diperluas. Topik ini hanya memodifikasi OnLaunched
.
Jika Anda tidak menggunakan templat proyek untuk aplikasi Anda, lihat Langkah 4: Mengubah handler aktivasi peluncuran untuk contoh yang dimodifikasi OnLaunched
yang tidak menggunakan navigasi Bingkai .
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();
}
}
}
Topik terkait
Referensi