2015-07-10 44 views
2

我遇到了麻烦,我无法解决。科尔多瓦/ Phonegap应用程序中的Windows Phone中的后退问题

我试图操纵的WindowsPhone的后退按钮,但它似乎没有工作

我试图重新使用一个事件(实际适用于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(); 
     } 
    } 

自似乎没有在Windows Phone上工作,经过一番研究,我已经通过的NuGet WinJS安装并试图调用函数这样

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

我在deviceready事件中调用此事件,所以我没有解决方案。

它似乎没有检测到事件甚至功能。所以我想知道我做错了什么。

任何消化?

回答

0

我希望你已经解决了你的问题,如果不是你可以尝试下面的方法。

您需要在Cordova项目的index.js文件中包含backbutton()函数。

您可以实现类似下面的东西,

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 
}; 
相关问题