雙擊退出
在 UWP 中可以調用如下方法對后退按鈕進行事件處理,比如頁面導航,退出全屏,雙擊退出等等。
// 注冊后退事件
SystemNavigationManager.GetForCurrentView().BackRequested += PageBackRequested;
// 后退事件處理
private void PageBackRequested(object sender, BackRequestedEventArgs e)
{
e.Handled = true;// 阻止后面注冊的事件繼續執行
// TODO
}
// 注銷
SystemNavigationManager.GetForCurrentView().BackRequested -= PageBackRequested;
通過上面的調用已經可以對后退按鈕定制不同的點擊效果,但是,還存在一個問題。應用中肯定會有很多的頁面,每個頁面對于后退按鈕的處理需求肯定會有所不同,多次注冊后退事件(連續注冊不注銷)是難免的,注冊后退事件實際上是把每個事件處理依次加入到一個事件隊列中去,每當點擊后退按鈕的時候,就會按照先后順序依次執行事件處理,因此就會存在后面處理和前面的處理產生沖突導致無法實現預期中的效果,還需要做一些費勁的特殊處理才能正常使用。
所以可以做一個簡單的封裝,實現只處理最后注冊的事件
using System;
using System.Collections;
using Windows.UI.Core;
namespace indi.anyesu.UWP.Core.Managers
{
//
// 自定義后退事件管理:
// 允許只調用最后注冊的后退事件,而SystemNavigationManager的后退事件是按順序依次執行的。
public sealed class BackEventManager
{
private static Stack BackEventStack = new Stack();
//
// 注冊后退事件
//
public static void Register(EventHandler<BackRequestedEventArgs> PageBackRequested)
{
if (BackEventStack.Count > 0)
{
SystemNavigationManager.GetForCurrentView().BackRequested -= BackEventStack.Peek() as EventHandler<BackRequestedEventArgs>;
}
SystemNavigationManager.GetForCurrentView().BackRequested += PageBackRequested;// 注冊到系統自帶的后退事件隊列
BackEventStack.Push(PageBackRequested);
}
//
// 注銷最后注冊的后退事件
//
public static void Unregister(EventHandler<BackRequestedEventArgs> PageBackRequested)
{
if (BackEventStack.Count > 0)
{
var top = BackEventStack.Peek() as EventHandler<BackRequestedEventArgs>;
if (PageBackRequested.Equals(top))
{
SystemNavigationManager.GetForCurrentView().BackRequested -= PageBackRequested;
BackEventStack.Pop();
if (BackEventStack.Count > 0)
{
SystemNavigationManager.GetForCurrentView().BackRequested += BackEventStack.Peek() as EventHandler<BackRequestedEventArgs>;
}
}
}
}
}
}
主要思路是將所有注冊的事件處理壓入 BackEventStack 這個棧當中,保證系統自帶的后退事件處理隊列當中最多只有一個事件處理,即最后注冊的那個。調用方法如下:
BackEventManager.Register(PageBackRequested);// 注冊后退事件
BackEventManager.Unregister(PageBackRequested);// 注銷后退事件
// 注意: 最好保證注冊和注銷成對出現(如在頁面的OnNavigatedTo方法中注冊,OnNavigatedFrom方法中注銷),避免出現沖突。
有了這個后退事件管理器之后,可以在 APP 初始化的時候完成后退事件的注冊,實現一個統一的后退事件處理,特殊頁面特殊處理,這樣就不用到處貼代碼了,維護起來更方便。
在 App.xaml.cs 中修改 OnLaunched 方法,如下所示:
/// <summary>
/// 在應用程序由最終用戶正常啟動時進行調用。
/// 將在啟動應用程序以打開特定文件等情況下使用。
/// </summary>
/// <param name="e">有關啟動請求和過程的詳細信息。</param>
protected override async void OnLaunched(LaunchActivatedEventArgs e)
{
Frame rootFrame = Window.Current.Content as Frame;
// 不要在窗口已包含內容時重復應用程序初始化,
// 只需確保窗口處于活動狀態
if (rootFrame == null)
{
// 創建要充當導航上下文的框架,并導航到第一頁
rootFrame = new Frame();
rootFrame.NavigationFailed += OnNavigationFailed;
// 下面這句話是關鍵
rootFrame.Navigated += OnNavigated;// 注冊頁面加載完畢事件
if (e.PreviousExecutionState == ApplicationExecutionState.Terminated)
{
//TODO: 從之前掛起的應用程序加載狀態
}
// 將框架放在當前窗口中
Window.Current.Content = rootFrame;
}
if (rootFrame.Content == null)
{
// 當導航堆棧尚未還原時,導航到第一頁,
// 并通過將所需信息作為導航參數傳入來配置
rootFrame.Navigate(typeof(MainPage), e.Arguments);
}
// 如果是移動端,則設置可以設置頂部狀態欄(電量、時間...)的顏色、是否顯示
if (Windows.Foundation.Metadata.ApiInformation.IsTypePresent("Windows.UI.ViewManagement.StatusBar"))
{
StatusBar statusBar = StatusBar.GetForCurrentView();
// statusBar.ForegroundColor = Colors.White;// 設置背景色
await statusBar.HideAsync();// 隱藏狀態欄
}
Window.Current.Activate();// 確保當前窗口處于活動狀態
}
// 在OnNavigated方法中進行事件的注冊和控制后退按鈕的顯示與否
private void OnNavigated(object sender, NavigationEventArgs e)
{
//顯示標題欄后退按鈕
//SystemNavigationManager.GetForCurrentView().AppViewBackButtonVisibility = ((Frame)sender).CanGoBack ? AppViewBackButtonVisibility.Visible : AppViewBackButtonVisibility.Collapsed;
SystemNavigationManager.GetForCurrentView().AppViewBackButtonVisibility = AppViewBackButtonVisibility.Visible;// 在PC客戶端左上角顯示后退按鈕
BackEventManager.Register(PageBackRequested);// 注冊后退事件
}
這里最關鍵的一句就是 rootFrame.Navigated += OnNavigated;
在 PageBackRequested 方法中做了統一的后退事件處理,比如在我的應用中,我希望顯示 MainPage 時后退按鈕能夠雙擊徹底退出應用,顯示其他頁面時后退按鈕能夠處理正常的頁面后退導航,具體處理如下:
private void PageBackRequested(object sender, BackRequestedEventArgs e)
{
e.Handled = true;
GoBack();
}
/// <summary>
/// 自定義全局后退事件處理
/// </summary>
public static async void GoBack()
{
var rootFrame = Window.Current.Content as Frame;// App的根Frame
if (rootFrame == null)
{
return;
}
if (rootFrame.CurrentSourcePageType == typeof(MainPage))// 判斷rootFrame 當前頁面類型是否為MainPage
{
if (!IsQuit)// IsQuit表示是否已點擊了后退按鈕,用來處理雙擊事件
{
IsQuit = true;
await (rootFrame.Content as MainPage).showMessage("再按一次返回鍵退出", Colors.Red);// 異步調出頁面的提示框
IsQuit = false;
}
else
{
Current.Exit();// 徹底退出App
}
}
else if (rootFrame.CanGoBack)
{
rootFrame.GoBack();
}
}
在MainPage中
public async Task showMessage(string msg, Color color)
{
Hint.Text = msg;// 設置提示框文本內容
if (color != null)
{
if (color == Colors.Green)
{
color = Color.FromArgb(255, 91, 159, 82);
}
messageBorder.Background = new SolidColorBrush(color);// 設置提示框背景色
}
message.Visibility = Visibility.Visible;// 顯示提示框
await Task.Delay(1500);// 延時1500ms
message.Visibility = Visibility.Collapsed;// 隱藏提示框
}
//以下為xaml內容,一個自定義的文本提示框
<Grid x:Name="message" RelativePanel.AlignVerticalCenterWithPanel="True" RelativePanel.AlignHorizontalCenterWithPanel="True" Visibility="Collapsed">
<Border x:Name="messageBorder" CornerRadius="10" Background="#5B9F52"></Border>
<ScrollViewer VerticalAlignment="Center" MaxHeight="120" VerticalScrollBarVisibility="Auto" BorderThickness="0">
<TextBlock x:Name="Hint" Foreground="White" RelativePanel.AlignHorizontalCenterWithPanel="True" RelativePanel.AlignVerticalCenterWithPanel="True" VerticalAlignment="Center" HorizontalAlignment="Center" TextAlignment="Center" Margin="0" TextWrapping="Wrap" Padding="10" MinWidth="120" MinHeight="30" FontFamily="Resources/FontAwesome.otf#FontAwesome" FontSize="16"/>
</ScrollViewer>
</Grid>
總結
在我的開發過程中,這個后退事件管理器已經基本滿足所有需求了,不過每次執行后退事件的時候都很任性的拋棄了之前注冊的事件,之后會考慮加入與前面注冊的事件共存的方法并做一些優化,以便靈活地適應更多的通用需求。