在 UWP 中可以調(diào)用如下方法對(duì)后退按鈕進(jìn)行事件處理党窜,比如頁面導(dǎo)航程梦,退出全屏点把,雙擊退出等等。
// 注冊(cè)后退事件
SystemNavigationManager.GetForCurrentView().BackRequested += PageBackRequested;
// 后退事件處理
private void PageBackRequested(object sender, BackRequestedEventArgs e)
{
e.Handled = true;// 阻止后面注冊(cè)的事件繼續(xù)執(zhí)行
// TODO
}
// 注銷
SystemNavigationManager.GetForCurrentView().BackRequested -= PageBackRequested;
通過上面的調(diào)用已經(jīng)可以對(duì)后退按鈕定制不同的點(diǎn)擊效果屿附,但是郎逃,還存在一個(gè)問題。應(yīng)用中肯定會(huì)有很多的頁面挺份,每個(gè)頁面對(duì)于后退按鈕的處理需求肯定會(huì)有所不同褒翰,多次注冊(cè)后退事件(連續(xù)注冊(cè)不注銷)是難免的,注冊(cè)后退事件實(shí)際上是把每個(gè)事件處理依次加入到一個(gè)事件隊(duì)列中去匀泊,每當(dāng)點(diǎn)擊后退按鈕的時(shí)候优训,就會(huì)按照先后順序依次執(zhí)行事件處理,因此就會(huì)存在后面處理和前面的處理產(chǎn)生沖突導(dǎo)致無法實(shí)現(xiàn)預(yù)期中的效果各聘,還需要做一些費(fèi)勁的特殊處理才能正常使用揣非。
所以可以做一個(gè)簡(jiǎn)單的封裝,實(shí)現(xiàn)只處理最后注冊(cè)的事件
using System;
using System.Collections;
using Windows.UI.Core;
namespace indi.anyesu.UWP.Core.Managers
{
//
// 自定義后退事件管理:
// 允許只調(diào)用最后注冊(cè)的后退事件伦吠,而SystemNavigationManager的后退事件是按順序依次執(zhí)行的妆兑。
public sealed class BackEventManager
{
private static Stack BackEventStack = new Stack();
//
// 注冊(cè)后退事件
//
public static void Register(EventHandler<BackRequestedEventArgs> PageBackRequested)
{
if (BackEventStack.Count > 0)
{
SystemNavigationManager.GetForCurrentView().BackRequested -= BackEventStack.Peek() as EventHandler<BackRequestedEventArgs>;
}
SystemNavigationManager.GetForCurrentView().BackRequested += PageBackRequested;// 注冊(cè)到系統(tǒng)自帶的后退事件隊(duì)列
BackEventStack.Push(PageBackRequested);
}
//
// 注銷最后注冊(cè)的后退事件
//
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>;
}
}
}
}
}
}
主要思路是將所有注冊(cè)的事件處理壓入 BackEventStack 這個(gè)棧當(dāng)中魂拦,保證系統(tǒng)自帶的后退事件處理隊(duì)列當(dāng)中最多只有一個(gè)事件處理,即最后注冊(cè)的那個(gè)搁嗓。調(diào)用方法如下:
BackEventManager.Register(PageBackRequested);// 注冊(cè)后退事件
BackEventManager.Unregister(PageBackRequested);// 注銷后退事件
// 注意: 最好保證注冊(cè)和注銷成對(duì)出現(xiàn)(如在頁面的OnNavigatedTo方法中注冊(cè)芯勘,OnNavigatedFrom方法中注銷),避免出現(xiàn)沖突腺逛。
有了這個(gè)后退事件管理器之后荷愕,可以在 APP 初始化的時(shí)候完成后退事件的注冊(cè),實(shí)現(xiàn)一個(gè)統(tǒng)一的后退事件處理棍矛,特殊頁面特殊處理安疗,這樣就不用到處貼代碼了,維護(hù)起來更方便够委。
在 App.xaml.cs 中修改 OnLaunched 方法荐类,如下所示:
/// <summary>
/// 在應(yīng)用程序由最終用戶正常啟動(dòng)時(shí)進(jìn)行調(diào)用。
/// 將在啟動(dòng)應(yīng)用程序以打開特定文件等情況下使用茁帽。
/// </summary>
/// <param name="e">有關(guān)啟動(dòng)請(qǐng)求和過程的詳細(xì)信息玉罐。</param>
protected override async void OnLaunched(LaunchActivatedEventArgs e)
{
Frame rootFrame = Window.Current.Content as Frame;
// 不要在窗口已包含內(nèi)容時(shí)重復(fù)應(yīng)用程序初始化,
// 只需確保窗口處于活動(dòng)狀態(tài)
if (rootFrame == null)
{
// 創(chuàng)建要充當(dāng)導(dǎo)航上下文的框架潘拨,并導(dǎo)航到第一頁
rootFrame = new Frame();
rootFrame.NavigationFailed += OnNavigationFailed;
// 下面這句話是關(guān)鍵
rootFrame.Navigated += OnNavigated;// 注冊(cè)頁面加載完畢事件
if (e.PreviousExecutionState == ApplicationExecutionState.Terminated)
{
//TODO: 從之前掛起的應(yīng)用程序加載狀態(tài)
}
// 將框架放在當(dāng)前窗口中
Window.Current.Content = rootFrame;
}
if (rootFrame.Content == null)
{
// 當(dāng)導(dǎo)航堆棧尚未還原時(shí)吊输,導(dǎo)航到第一頁,
// 并通過將所需信息作為導(dǎo)航參數(shù)傳入來配置
rootFrame.Navigate(typeof(MainPage), e.Arguments);
}
// 如果是移動(dòng)端铁追,則設(shè)置可以設(shè)置頂部狀態(tài)欄(電量季蚂、時(shí)間...)的顏色、是否顯示
if (Windows.Foundation.Metadata.ApiInformation.IsTypePresent("Windows.UI.ViewManagement.StatusBar"))
{
StatusBar statusBar = StatusBar.GetForCurrentView();
// statusBar.ForegroundColor = Colors.White;// 設(shè)置背景色
await statusBar.HideAsync();// 隱藏狀態(tài)欄
}
Window.Current.Activate();// 確保當(dāng)前窗口處于活動(dòng)狀態(tài)
}
// 在OnNavigated方法中進(jìn)行事件的注冊(cè)和控制后退按鈕的顯示與否
private void OnNavigated(object sender, NavigationEventArgs e)
{
//顯示標(biāo)題欄后退按鈕
//SystemNavigationManager.GetForCurrentView().AppViewBackButtonVisibility = ((Frame)sender).CanGoBack ? AppViewBackButtonVisibility.Visible : AppViewBackButtonVisibility.Collapsed;
SystemNavigationManager.GetForCurrentView().AppViewBackButtonVisibility = AppViewBackButtonVisibility.Visible;// 在PC客戶端左上角顯示后退按鈕
BackEventManager.Register(PageBackRequested);// 注冊(cè)后退事件
}
這里最關(guān)鍵的一句就是 rootFrame.Navigated += OnNavigated;
在 PageBackRequested 方法中做了統(tǒng)一的后退事件處理琅束,比如在我的應(yīng)用中扭屁,我希望顯示 MainPage 時(shí)后退按鈕能夠雙擊徹底退出應(yīng)用,顯示其他頁面時(shí)后退按鈕能夠處理正常的頁面后退導(dǎo)航涩禀,具體處理如下:
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 當(dāng)前頁面類型是否為MainPage
{
if (!IsQuit)// IsQuit表示是否已點(diǎn)擊了后退按鈕疯搅,用來處理雙擊事件
{
IsQuit = true;
await (rootFrame.Content as MainPage).showMessage("再按一次返回鍵退出", Colors.Red);// 異步調(diào)出頁面的提示框
IsQuit = false;
}
else
{
Current.Exit();// 徹底退出App
}
}
else if (rootFrame.CanGoBack)
{
rootFrame.GoBack();
}
}
在MainPage中
public async Task showMessage(string msg, Color color)
{
Hint.Text = msg;// 設(shè)置提示框文本內(nèi)容
if (color != null)
{
if (color == Colors.Green)
{
color = Color.FromArgb(255, 91, 159, 82);
}
messageBorder.Background = new SolidColorBrush(color);// 設(shè)置提示框背景色
}
message.Visibility = Visibility.Visible;// 顯示提示框
await Task.Delay(1500);// 延時(shí)1500ms
message.Visibility = Visibility.Collapsed;// 隱藏提示框
}
//以下為xaml內(nèi)容,一個(gè)自定義的文本提示框
<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>
總結(jié)
在我的開發(fā)過程中埋泵,這個(gè)后退事件管理器已經(jīng)基本滿足所有需求了,不過每次執(zhí)行后退事件的時(shí)候都很任性的拋棄了之前注冊(cè)的事件罪治,之后會(huì)考慮加入與前面注冊(cè)的事件共存的方法并做一些優(yōu)化丽声,以便靈活地適應(yīng)更多的通用需求。