在開發 Windows 8 Store Apps 時,除了核心功能實作,還有許多細節能大幅提升使用者體驗。本文將介紹五個實用技巧:自定義啟動畫面、偵測應用程式執行環境(本機、遠端桌面或模擬器)、透過 DisplayRequest 保持螢幕常亮、使用 MessageDialog 顯示訊息對話框,以及實作 PopupMenu 內容選單。這些功能雖屬「其它」類別,卻是打造專業級應用的關鍵細節。
Windows 8 預設啟動畫面僅支援單一圖片加背景色,無法直接客製化。但我們可以在預設啟動畫面關閉後,立即顯示一個高度客製化的「延伸啟動畫面」,並在其中執行初始化工作(如載入資料、檢查更新),完成後再導向主頁面。
Page 的自定義啟動頁面(例如 MySplashScreen.xaml),版面配置參考系統啟動畫面的圖片位置(SplashScreen.ImageLocation),確保視覺無縫銜接。App.xaml.cs 的 OnLaunched 事件中,若應用非處於執行中狀態,則實例化自定義啟動頁並設為 Window.Current.Content。Window.Current.SizeChanged 事件以應對螢幕旋轉或縮放,並啟動非同步初始化流程(如 Task.Delay 模擬耗時操作)。Frame 導航至主頁面(MainPage),並呼叫 Window.Current.Activate() 啟動視窗。// App.xaml.cs
protected async override void OnLaunched(LaunchActivatedEventArgs args) {
if (args.PreviousExecutionState != ApplicationExecutionState.Running) {
var mySplashScreen = new XamlDemo.Feature.MySplashScreen(args);
Window.Current.Content = mySplashScreen;
}
Window.Current.Activate();
}
// MySplashScreen.xaml.cs 部分邏輯
private async void ImplementCustomSplashScreen(LaunchActivatedEventArgs args) {
Window.Current.SizeChanged += Current_SizeChanged;
_splashScreen = args.SplashScreen;
_splashScreen.Dismissed += splashScreen_Dismissed;
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();
}
透過 Windows.System.RemoteDesktop.InteractiveSession.IsRemote 屬性,即可判斷應用程式目前是否運行於遠端桌面或模擬器環境。此屬性為 true 表示遠端/模擬器,false 表示本機執行。這對於調整 UI 佈局、啟用/停用特定功能(如觸控優化)非常有用。
protected override void OnNavigatedTo(NavigationEventArgs e) {
lblMsg.Text = "Windows.System.RemoteDesktop.InteractiveSession.IsRemote: "
+ Windows.System.RemoteDesktop.InteractiveSession.IsRemote;
}
DisplayRequest 實戰當應用播放影片、進行導航或需要長時間顯示內容時,應防止系統自動關閉螢幕。DisplayRequest 類別提供 RequestActive() 與 RequestRelease() 方法來控制螢幕狀態。最佳實踐:在播放媒體時呼叫 RequestActive(),暫停或停止時呼叫 RequestRelease(),並妥善處理物件生命週期避免記憶體洩漏。
private DisplayRequest dispRequest = null;
private void btnPlay_Click(object sender, RoutedEventArgs e) {
mediaElement.Play();
if (dispRequest == null) {
dispRequest = new DisplayRequest();
dispRequest.RequestActive();
lblMsg.Text += "螢幕將保持點亮狀態" + Environment.NewLine;
}
}
private void btnPause_Click(object sender, RoutedEventArgs e) {
mediaElement.Pause();
if (dispRequest != null) {
dispRequest.RequestRelease();
dispRequest = null;
lblMsg.Text += "螢幕恢復正常休眠策略" + Environment.NewLine;
}
}
MessageDialog:標準化訊息對話框MessageDialog 是 Windows 8 取代傳統 MessageBox 的新 API,支援非同步顯示、自訂按鈕、預設/取消按鈕指定,且符合 Modern UI 風格。使用 await dialog.ShowAsync() 即可顯示並等待使用者回應。
PopupMenu:內容選單(右鍵/長按選單)PopupMenu 提供輕量級的內容選單,支援命令按鈕、分隔線,並可在指定座標(如滑鼠右鍵位置或觸控長按點)彈出。透過 UICommand 綁定標籤、動作與參數,並以 ShowAsync(Point) 或 ShowForSelectionAsync(Rect) 顯示。
async void lblDemo_RightTapped(object sender, RightTappedRoutedEventArgs e) {
var menu = new PopupMenu();
menu.Commands.Add(new UICommand("項目1", cmd =>
lblMsg.Text = $"label:{cmd.Label}, commandId:{cmd.Id}", "param1"));
menu.Commands.Add(new UICommand("項目2", cmd =>
lblMsg.Text = $"label:{cmd.Label}, commandId:{cmd.Id}", "param2"));
menu.Commands.Add(new UICommandSeparator());
menu.Commands.Add(new UICommand("項目3", cmd =>
lblMsg.Text = $"label:{cmd.Label}, commandId:{cmd.Id}", "param3"));
var chosen = await menu.ShowAsync(e.GetPosition(this));
// 亦可用 ShowForSelectionAsync 針對選取區域顯示
}
掌握這些「其它」功能,能讓你的 Windows 8 Store Apps 在啟動體驗、環境適應性、媒體播放、使用者互動等細節上更臻完善。建議在專案早期規劃並統一封裝這些工具類方法,以提高開發效率與程式碼可維護性。