“backbutton” event won't fire

孤街浪徒 提交于 2019-12-23 10:27:01

问题


I am attempting to build a phonegap app for Windows Phone 7. I am trying to follow the documentation for the "backbutton" event (http://docs.phonegap.com/en/2.0.0/cordova_events_events.md.html#backbutton), but I can't seem to get it to work.

The "deviceready" event fires, but the "backbutton" event does not. When compiling and running in Visual Studio Windows Phone emulator the onDeviceReady function is called and "Device ready" is logged, but when the emulator back button is pressed the application exits and nothing is logged in the console. When the back button is pressed the OnBackKeyDown function should run.

copy of the code from the offical doc:

<html>
    <head>     
        <script type="text/javascript" charset="utf-8" src="cordova-2.0.0.js"></script>
        <script type="text/javascript">
            function onLoad() {
                document.addEventListener("deviceready", onDeviceReady, false);
            }
            function onDeviceReady() {
                console.log("Device ready");
                document.addEventListener("backbutton", onBackKeyDown, false);
            }
            function onBackKeyDown() {
                console.log("Back button pressed");
            }
        </script>
    </head>
    <body onload="onLoad()">
        <div></div>
    </body>
</html>

回答1:


The fix has been committed here: https://github.com/purplecabbage/incubator-cordova-wp7/commit/d04b87abb3c3822ef25438e1353a1d7d2e0d6628

You will need to wait for 2.1.0 to be released early next week, or build your app from source-code, in the meantime.




回答2:


I managed to fix this by copying some parts of cordova-1.8.1.js to cordova-2.0.0.js.

In 1.8.1, search for: var NamedArgs and copy the whole object to 2.0.0.

In 2.0.0, search for: var command = service + "/" + action + "/" + callbackId + "/" + JSON.stringify(args); and replace it with:

if ( action == 'overridebackbutton' ) {
    if ( NamedArgs[service] && NamedArgs[service][action]) {
        var argNames = NamedArgs[service][action];
        var newArgs = {};
        var len = Math.min(args.length,argNames.length);

        for(var n = 0; n < len; n++) {
            newArgs[argNames[n]] = args[n];
        }

        args = newArgs;
    }
    else if(args && args.length && args.length == 1) {
        args = args[0];
    }
}
var command = service + "/" + action + "/" + callbackId + "/" + JSON.stringify(args);

This might not be a pretty solution, but it works for me.



来源:https://stackoverflow.com/questions/11866952/backbutton-event-wont-fire

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!