天天看点

实现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退出按键重写