簡體   English   中英

Cordova / Phonegap應用程序上的Windows Phone中后退按鈕出現問題

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

我遇到了無法解決的麻煩。

我正在嘗試操縱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事件中調用此事件,所以我沒有解決方案。

似乎它沒有檢測到事件甚至功能。 所以我想知道我在做什么錯。

有什么建議嗎?

希望您已解決您的問題,否則請嘗試以下方法。

您需要在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
};

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM