Cordova windows 10 在软件关闭按钮/alt+F4 上添加确认消息

Cordova windows 10 add confirm message on software close button / alt+F4

当用户在 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

关闭按钮被限制 Capabilities 函数启用此功能

打开 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">

在 Capabilities 标签中添加 <rescap:Capability Name="confirmAppClose" />

<Capabilities> <rescap:Capability Name="confirmAppClose" /> </Capabilities>

此处 xmlns:rescap="http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapaIgnorableNamespaces="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();
        }