当用户单击运行在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');
}谢谢
发布于 2018-04-18 09:26:51
步骤: 1
关闭按钮是限制功能功能,以启用
打开package.windows10.appxmanifest上的platform -> windows文件夹。
步骤: 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();
}https://stackoverflow.com/questions/46832073
复制相似问题