首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> .NET 網頁程式設計 >> 關於.NET

Windows 8 Store Apps 開發指南 (69):自定義啟動畫面、執行環境檢測與螢幕常亮技巧

     編輯:關於.NET

前言

在開發 Windows 8 Store Apps 時,除了核心功能實作,還有許多細節能大幅提升使用者體驗。本文將介紹五個實用技巧:自定義啟動畫面、偵測應用程式執行環境(本機、遠端桌面或模擬器)、透過 DisplayRequest 保持螢幕常亮、使用 MessageDialog 顯示訊息對話框,以及實作 PopupMenu 內容選單。這些功能雖屬「其它」類別,卻是打造專業級應用的關鍵細節。

一、自定義啟動畫面:打造專屬品牌體驗

Windows 8 預設啟動畫面僅支援單一圖片加背景色,無法直接客製化。但我們可以在預設啟動畫面關閉後,立即顯示一個高度客製化的「延伸啟動畫面」,並在其中執行初始化工作(如載入資料、檢查更新),完成後再導向主頁面。

核心步驟

  1. 建立一個繼承自 Page 的自定義啟動頁面(例如 MySplashScreen.xaml),版面配置參考系統啟動畫面的圖片位置(SplashScreen.ImageLocation),確保視覺無縫銜接。
  2. 在 App.xaml.cs 的 OnLaunched 事件中,若應用非處於執行中狀態,則實例化自定義啟動頁並設為 Window.Current.Content。
  3. 自定義頁面載入後,註冊 Window.Current.SizeChanged 事件以應對螢幕旋轉或縮放,並啟動非同步初始化流程(如 Task.Delay 模擬耗時操作)。
  4. 初始化完成後,建立新的 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 在啟動體驗、環境適應性、媒體播放、使用者互動等細節上更臻完善。建議在專案早期規劃並統一封裝這些工具類方法,以提高開發效率與程式碼可維護性。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved