当应用程序加载时,如果耗费的事件比较长,可以自定义 SplashScreen ,来延长加载过程。
系统的 SplashScreen 类:
namespace Windows.ApplicationModel.Activation { // 为应用程序的初始屏幕提供关闭事件和屏幕位置信息。 public sealed class SplashScreen { // 相对于窗体的应用程序初始屏幕图像的坐标。 // 返回结果: 相对于窗体并且针对设备的点/英寸 (dpi) 的初始屏幕图像的坐标。 public Rect ImageLocation { get; } // 当关闭应用程序的初始屏幕时激发。 public event TypedEventHandler<SplashScreen, object> Dismissed; } }
1、 App.xaml.cs 文件中的 protected override void OnLaunched(LaunchActivatedEventArgs args) 方法,形参 args 中包含默认
SplashScreen 的引用 :args.SplashScreen。
首先自定义一个 “闪屏页面”( ExtendedSplash ):
XAML :
<Grid Background="#00b2f0" x:Class="SplashScreenSample.ExtendedSplash" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" d:DesignWidth="1366" d:DesignHeight="768"> <Grid.RowDefinitions> <RowDefinition/> <RowDefinition Height="180"/> </Grid.RowDefinitions> <Canvas Grid.Row="0"> <Image x:Name="extendedSplashImage" Source="Assets/splash-sdk.png" /> </Canvas> <StackPanel Grid.Row="1" HorizontalAlignment="Center"> <TextBlock Style="{StaticResource BasicTextStyle}" TextWrapping="Wrap" TextAlignment="Center" Padding="5" HorizontalAlignment="Center"> The splash screen was dismissed and the image above was positioned using the splash screen API. </TextBlock> <Button x:Name="LearnMoreButton" Content="Learn More" HorizontalAlignment="Center" /> </StackPanel> </Grid>
相应的 C# 页面 :
namespace SplashScreenSample { partial class ExtendedSplash { //存储应用默认 “闪屏” 的位置 internal Rect splashImageRect; //跟踪闪屏的消失状态 internal bool dismissed = false; internal Frame rootFrame; //引用到应用默认的闪屏对象 private SplashScreen splash; public ExtendedSplash(SplashScreen splashscreen, bool loadState) { InitializeComponent(); LearnMoreButton.Click += new RoutedEventHandler(LearnMoreButton_Click); // 侦听窗口尺寸改变事件,从而重置闪屏图片的位置 Window.Current.SizeChanged += new WindowSizeChangedEventHandler(ExtendedSplash_OnResize); splash = splashscreen; if (splash != null) { // 当关闭应用程序的初始屏幕时激发。 splash.Dismissed += new TypedEventHandler<SplashScreen, Object>(DismissedEventHandler); //获取闪屏的默认图片 splashImageRect = splash.ImageLocation; PositionImage(); } //初始化一个 Frame 对象,作为默认导航的上下文 rootFrame = new Frame(); //需要时,恢复保存的会话状态 RestoreStateAsync(loadState); } async void RestoreStateAsync(bool loadState) { if (loadState) await SuspensionManager.RestoreAsync(); } // 把扩展的闪屏图片放在默认闪屏图片的位置 void PositionImage() { extendedSplashImage.SetValue(Canvas.LeftProperty, splashImageRect.X); extendedSplashImage.SetValue(Canvas.TopProperty, splashImageRect.Y); extendedSplashImage.Height = splashImageRect.Height; extendedSplashImage.Width = splashImageRect.Width; } void ExtendedSplash_OnResize(Object sender, WindowSizeChangedEventArgs e) { if (splash != null) { splashImageRect = splash.ImageLocation; PositionImage(); } } void LearnMoreButton_Click(object sender, RoutedEventArgs e) { // 当点击按钮后,导航到 MainPage rootFrame.Navigate(typeof(MainPage)); //把 extended splash 对象保存到 MainPage 中的引用 ((MainPage)rootFrame.Content).SetExtendedSplashInfo(splashImageRect, dismissed); Window.Current.Content = rootFrame; } // 当系统闪屏消失,导航到当前闪屏时触发 void DismissedEventHandler(SplashScreen sender, object e) { dismissed = true; } } }
在 App.xaml.cs 文件中的 OnLaunched() 方法中,初始化并显示闪屏:
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(); }
2、获取闪屏图像的坐标。
显示结果的 XAML :
<TextBlock Style="{StaticResource BasicTextStyle}" TextWrapping="Wrap"> The splash screen was positioned at <Run FontWeight="Bold" Text="x:"/><Run x:Name="XValue"/> <Run FontWeight="Bold" Text="y:"/><Run x:Name="YValue"/> <Run FontWeight="Bold" Text=""/><Run x:Name="WidthValue"/> <Run FontWeight="Bold" Text="height:"/><Run x:Name="HeightValue"/> </TextBlock>
相应的 C# :
protected override void OnNavigatedTo(NavigationEventArgs e) { //在 MainPage 页面中,保存系统的 SplashScreen 位置的引用 Rect rect = MainPage.Current.SplashImageRect; XValue.Text = " " + rect.X.ToString() + ", "; YValue.Text = " " + rect.Y.ToString() + " with "; WidthValue.Text = " " + rect.Width.ToString() + ", "; HeightValue.Text = " " + rect.Height.ToString() + "."; }