当用户单击在Windows 10(桌面)上运行的Cordova应用程序的软件关闭按钮(X/Alt + F4)时,如何显示确认消息。我尝试了几件事,但没有任何效果:
//This only fire when clicking on the back arrow.
document.addEventListener("backbutton", onBackKeyDown.bind(this), false);
function onBackKeyDown(e) {
navigator.notification.alert('onBackKeyDown');
}
//This fire but to late and cannot cancel or display message
document.addEventListener( 'pause', onPause.bind( this ), false );
function onPause() {
debugger;
navigator.notification.alert('onPause');
};
//This is never fired
WinJS.Application.addEventListener("unload", unloadEv);
function unloadEv(ev) {
navigator.notification.alert('unloadEv');
}
//This is never fired
window.onbeforeunload = onbeforeunload;
function onbeforeunload(evt) {
navigator.notification.alert('onbeforeunload');
}
谢谢
步骤 : 1
关闭按钮受限 功能功能启用此功能
打开platform -> windows
文件夹上的package.windows10.appxmanifest
。
步骤 : 2
在该 xml 包标记中应如下所示
<Package IgnorableNamespaces="uap mp rescap" xmlns="http://schemas.microsoft.com/appx/manifest/foundation/windows10" xmlns:mp="http://schemas.microsoft.com/appx/2014/phone/manifest" xmlns:rescap="http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities" xmlns:uap="http://schemas.microsoft.com/appx/manifest/uap/windows10">
在"功能"标记中添加<rescap:Capability Name="confirmAppClose" />
<Capabilities>
<rescap:Capability Name="confirmAppClose" />
</Capabilities>
此处xmlns:rescap="http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapa
和IgnorableNamespaces="rescap"
是启用受限功能。
步骤 : 3
并在您的 js 文件中添加下面的 javascript 代码并构建
Windows.UI.Core.Preview.SystemNavigationManagerPreview.getForCurrentView().oncloserequested = function (args) {
args.detail[0].handled = true;
var message = new Windows.UI.Popups.MessageDialog("Details is not Saved Do you want save or exit the application..?");
message.commands.append(new Windows.UI.Popups.UICommand("Save", SaveHandler));
message.commands.append(new Windows.UI.Popups.UICommand("Exit", UnsaveHandler));
message.commands.append(new Windows.UI.Popups.UICommand("Cancel", CancelHandler));
message.showAsync();
}
function SaveHandler(command) {
//save button
}
function CancelHandler(command){
return false;
}
function UnsaveHandler(command) {
window.close();
}