天天看點

實作windows phone8.1退出按鍵重寫

實作windows phone8.1退出按鍵重寫

Hi,大家好。

移動開發中,像Android開發,在使用者點選後退按鈕時候,一般會彈出一個彈出框,提示使用者是否确定退出遊戲,或者提示再按一次退出遊戲(在一定時間内連續點選兩次實作退出),再或者是接入的sdk裡面的退出,帶廣告那種。

注:下面代碼隻使用windows phone8.1

windows phone開發中,也是一樣的原理。windows phone的後退也是有一個退出的。但是它的退出是把應用最小話,回到首頁面,程序還存在。

實作windows phone8.1退出按鍵重寫

因為此原因,增加一個自己的退出實作。調用window phone8.1API裡的彈出框就ok了。提供一個MessageDialog Class連結: 點選打開連結

下面是代碼實作:

實作windows phone8.1退出按鍵重寫

如果你的工程是wp8.1 universal c++ DX工程的話,那麼下面代碼直接搬過去就好了。在應用啟動的建立加載主要windows 父視窗的檔案中,一般會有一個叫做OnPageLoaded類似的方法。一定會有該檔案,一定會有加載該視窗的方法。

頭檔案實作:

實作windows phone8.1退出按鍵重寫

#if WINAPI_FAMILY==WINAPI_FAMILY_PHONE_APP

Windows::Foundation::EventRegistrationToken _backPressedEventToken;

void HardwareButton_BackPressed(Platform::Object^ sender,

Windows::Phone::UI::Input::BackPressedEventArgs^ e);

void CommandInvokedHandler(Windows::UI::Popups::IUICommand^ command); //監聽按鈕點選

#endif

cpp檔案實作:

實作windows phone8.1退出按鍵重寫

using namespace Windows::UI::Popups; //使用 windows 彈出框

#if WINAPI_FAMILY==WINAPI_FAMILY_PHONE_APP

using namespace Windows::Phone::UI::Input;

#endif

在OnPageLoaded方法中添加下面代碼:

#if WINAPI_FAMILY==WINAPI_FAMILY_PHONE_APP

_backPressedEventToken = HardwareButtons::BackPressed +=

ref new EventHandler<BackPressedEventArgs^>(this,

&OpenGLESPage::HardwareButton_BackPressed);

#endif

在cpp代碼區域再添加下面代碼:

代碼開始。

實作windows phone8.1退出按鍵重寫

#if WINAPI_FAMILY==WINAPI_FAMILY_PHONE_APP

/// <summary>

/// 處理後退按鈕按下事件并在根架構的曆史記錄中導航。 huajian.tian add in 2015年8月20日15:08:55

/// </summary>

void OpenGLESPage::HardwareButton_BackPressed(Object^ sender, BackPressedEventArgs^ e)

{

//首先下面第一句話是必須的,否則應用程式就會直接進入背景界面。

e->Handled = true;

// Create the message dialog and set its content

//MessageDialog^ dig = ref new MessageDialog("Are you sure you want to close this app?", "Confirm");

MessageDialog^ dig = ref new MessageDialog("是否退出本程式?", "提示");

// Add commands and set their callbacks.

// Both commands use the same callback function instead of inline event handlers.

UICommand^ btnOk = ref new UICommand(

"确定", 

ref new UICommandInvokedHandler(this, &OpenGLESPage::CommandInvokedHandler));

UICommand^ btnCancel = ref new UICommand(

"取消", 

ref new UICommandInvokedHandler(this, &OpenGLESPage::CommandInvokedHandler));

// Add the commands to the dialog

dig->Commands->Append(btnOk);

dig->Commands->Append(btnCancel);

// Set the command that will be invoked by default

dig->DefaultCommandIndex = 0;

// Set the command to be invoked when escape is pressed

dig->CancelCommandIndex = 1;

// Show the message dialog

dig->ShowAsync();

return;

}

void OpenGLESPage::CommandInvokedHandler(Windows::UI::Popups::IUICommand^ command)

{

// Display message

printf("command->Label = %s",command->Label);

if (command->Label == "确定")

{

//退出應用程式

Windows::ApplicationModel::Core::CoreApplication::Exit();

}

else if(command->Label == "取消")

{

//No deal

}

}

#endif

代碼結束。

實作windows phone8.1退出按鍵重寫

上面HardwareButton_BackPressed這個方法中  e->Handled = true;這句話是必須的,意思是不會退出應用,如果沒有這句話, 預設為false,應用就直接切入到背景了。

加這句話提供了攔截作用,告訴底層代碼,我不要在這裡進入背景。

最後謝謝大家。

實作windows phone8.1退出按鍵重寫