2015-07-10 74 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 
}; 
相關問題