重新想象 Windows 8 Store Apps (69) - 其它: 自訂啟動螢幕, 程式的運行位置, 保持螢幕的點亮狀態, MessageDialog, PopupMenu
作者:webabcd
介紹
重新想象 Windows 8 Store Apps 之 其它
自訂啟動螢幕
檢查當前呈現的應用程式是運行在本地還是運行在遠端桌面或模擬器
保持螢幕的點亮狀態
MessageDialog - 資訊對話方塊
PopupMenu - 操作功能表
樣本
1、示範如何自訂啟動螢幕
Feature/MySplashScreen.xaml
<Page x:Class="XamlDemo.Feature.MySplashScreen" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:XamlDemo.Feature" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d"> <Grid Background="#1b4b7c"> <Grid.RowDefinitions> <RowDefinition /> <RowDefinition Height="180" /> </Grid.RowDefinitions> <Canvas Grid.Row="0"> <Image x:Name="splashImage" Source="/Assets/SplashScreen.png" /> </Canvas> <StackPanel Grid.Row="1" HorizontalAlignment="Center"> <ProgressRing IsActive="True" /> <TextBlock Name="lblMsg" Text="我是自訂啟動頁,1 秒後跳轉到首頁" FontSize="14.667" Margin="0 10 0 0" /> </StackPanel> </Grid></Page>
Feature/MySplashScreen.xaml.cs
/* * 示範如何自訂啟動螢幕 * 關聯代碼:App.xaml.cs 中的 override void OnLaunched(LaunchActivatedEventArgs args) * * 應用情境: * 1、app 的啟動螢幕就是一個圖片加一個背景色,其無法更改, * 2、但是啟動螢幕過後可以參照啟動螢幕做一個內容更豐富的自訂啟動螢幕 * 3、在自訂啟動螢幕顯示後,可以做一些程式的初始化工作,初始化完成後再進入主程式 */ using System.Threading.Tasks;using Windows.ApplicationModel.Activation;using Windows.Foundation;using Windows.UI.Xaml;using Windows.UI.Xaml.Controls; namespace XamlDemo.Feature{ public sealed partial class MySplashScreen : Page { /* * SplashScreen - app 的啟動螢幕對象,在 Application 中的若干事件處理器中的事件參數中均可獲得 * ImageLocation - app 的啟動螢幕的圖片的位置資訊,返回 Rect 類型對象 * Dismissed - app 的啟動螢幕關閉時所觸發的事件 */ // app 啟動螢幕的相關資訊 private SplashScreen _splashScreen; public MySplashScreen() { this.InitializeComponent(); lblMsg.Text = "自訂 app 的啟動螢幕,開啟 app 時可看到此頁面的示範"; } // LaunchActivatedEventArgs 來源於 App.xaml.cs 中的 override void OnLaunched(LaunchActivatedEventArgs args) public MySplashScreen(LaunchActivatedEventArgs args) { this.InitializeComponent(); ImplementCustomSplashScreen(args); } private async void ImplementCustomSplashScreen(LaunchActivatedEventArgs args) { // 視窗尺寸發生改變時,重新調整自訂啟動螢幕 Window.Current.SizeChanged += Current_SizeChanged; // 擷取 app 的啟動螢幕的相關資訊 _splashScreen = args.SplashScreen; // app 的啟動螢幕關閉時所觸發的事件 _splashScreen.Dismissed += splashScreen_Dismissed; // 擷取 app 啟動螢幕的圖片的位置,並按此位置調整自訂啟動螢幕的圖片的位置 Rect splashImageRect = _splashScreen.ImageLocation; splashImage.SetValue(Canvas.LeftProperty, splashImageRect.X); splashImage.SetValue(Canvas.TopProperty, splashImageRect.Y); splashImage.Height = splashImageRect.Height; splashImage.Width = splashImageRect.Width; await Task.Delay(1000); // 關掉自訂啟動螢幕,跳轉到程式首頁面 var rootFrame = new Frame(); rootFrame.Navigate(typeof(MainPage), args); Window.Current.Content = rootFrame; Window.Current.Activate(); } void Current_SizeChanged(object sender, Windows.UI.Core.WindowSizeChangedEventArgs e) { // 擷取 app 啟動螢幕的圖片的最新位置,並按此位置調整自訂啟動螢幕的圖片的位置 Rect splashImageRect = _splashScreen.ImageLocation; splashImage.SetValue(Canvas.LeftProperty, splashImageRect.X); splashImage.SetValue(Canvas.TopProperty, splashImageRect.Y); splashImage.Height = splashImageRect.Height; splashImage.Width = splashImageRect.Width; } void splashScreen_Dismissed(SplashScreen sender, object args) { } }}
App.xaml.cs
protected async override void OnLaunched(LaunchActivatedEventArgs args){ // 顯示自訂啟動螢幕,參見 Feature/MySplashScreen.xaml.cs if (args.PreviousExecutionState != ApplicationExecutionState.Running) { XamlDemo.Feature.MySplashScreen mySplashScreen = new XamlDemo.Feature.MySplashScreen(args); Window.Current.Content = mySplashScreen; } // 確保當前視窗處於活動狀態 Window.Current.Activate();}
2、示範如何檢查當前呈現的應用程式是運行在本地還是運行在遠端桌面或模擬器
Feature/CheckRunningSource.xaml.cs