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

时间:2017-10-19 14:16:58

标签: cordova uwp winjs cordova-win10

当用户单击在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 个答案:

答案 0 :(得分:0)

步骤: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/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();
        }