简体   繁体   English

Cordova / Phonegap应用程序上的Windows Phone中后退按钮出现问题

[英]Trouble with backbutton in Windows Phone on Cordova/Phonegap app

I'm experience a trouble that i can't solve. 我遇到了无法解决的麻烦。

I'm trying to manipulate the backbutton of a WindowsPhone but it doesn't seem to work 我正在尝试操纵WindowsPhone的后退按钮,但似乎不起作用

I've tried to re-use an event (that actually works on Android app), that its more less like this 我试图重用一个事件(该事件实际上可在Android应用程序上运行),但它的作用不像这样

document.addEventListener("backbutton", onBackKeyDown, false);

function onBackKeyDown(e) {
        var location = window.location.href;
        console.log(location);
        var partial_location = location.split("#");
        if (partial_location[1] == "menu/" || partial_location[1] == "login/") {
            e.preventDefault();
            navigator.app.exitApp();
        } else {
            navigator.app.backHistory();
        }
    }

Since its doesnt seem to work on windows phone and after some research i've installed via nuget WinJS and tried to call the function like this 由于它似乎无法在Windows Phone上运行,经过一些研究,我已经通过nuget WinJS安装并尝试调用此函数

if (device.platform == "windows") {
        WinJS.Application.onbackclick = function(evt){
            onBackKeyDown(evt);
            return true;
        }
    } else {
        document.addEventListener("backbutton", onBackKeyDown, false);
    }

I'm calling this event inside the deviceready event, so I'm out of solutions. 我在deviceready事件中调用此事件,所以我没有解决方案。

It seems that it doesnt detect the event or even the function. 似乎它没有检测到事件甚至功能。 So I'm wondering what I'm doing wrong. 所以我想知道我在做什么错。

Any sugestion? 有什么建议吗?

I hope you have fixed your issue, If not you can try below method. 希望您已解决您的问题,否则请尝试以下方法。

You need to include the backbutton() function in index.js file of the Cordova project. 您需要在Cordova项目的index.js文件中包含backbutton()函数。

You can implement something similar to below, 您可以实现类似于以下内容的内容,

var app = {
    // Application Constructor
    initialize: function() {
        this.bindEvents();
    },
    // Bind event listeners for 'deviceready' and 'backbutton' 
    bindEvents: function() {
        document.addEventListener('deviceready', this.onDeviceReady, false);
        // Here define your back button listener
        document.addEventListener('backbutton', this.handleBackButton, false);
        // If you need to support native back button you can do like this
        // document.addEventListener('backbutton', this.handleBackButton, true);
    },

    // deviceready Event Handler
    onDeviceReady: function () {
        //OnDeviceReady function implementation    
    },

    // backbutton Event handler
    // Here define your logics
    handleBackButton: function () {
        // Check if you are in home page
        // You need to check according to your application. For example if the home page (data page) is index-in, show alert message
        if ($('.page-on-center').data('page') == "index-in") {
            myApp.confirm('Are you sure you want to exit ?', 'Exit',
              function () {
                  // If Click EXIT or Confirm
                  // This is not work for me
                  //navigator.navigation.exitApp();
                  // This works fine for me
                  navigator.app.exitApp();
              },
              function () {
                  // If click cancel do nothing
              }
            );
        } else {
            // Go to previous page
            mainView.router.back();
        }
    },
// do something other than the default, like undo an operation,
// or step backwards through a multi-step operation
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM