2016-03-22 4 views
1

Ich bin eine Einzelseite App für Windows Phone 8.1 in PhoneGap (HTML5 und Javascript) zu entwickeln. Everthing funktioniert gut, aber die einzige Problem kommt in der Handhabung der Zurück-Taste meiner App.Zurück-Taste Problem in Windows 8.1 app (PhoneGap)

Hier ist der Code Struktur meiner app (Single Seite App):

<body> 

<div class="Page1"> 
    // Here is the code of First page of my app (Login Wizard.) 
</div> 

<div class="Page2"> 
    // Here is the code of Second page of my app 
</div> 

<div class="Page3"> 
    // Here is the code of Third page of my app 
</div> 

<div class="Page4"> 
    // Here is the code of Fourth page of my app 
</div> 

<div class="Page15"> 
    // Here is the code of Fifth page of my app 
</div> 

<div class="Page6"> 
    // Here is the code of Sixth page of my app 
</div> 

//And so on 

</body> 

enter image description here

Ich habe versucht, mit verschiedenen Methoden, es zu handhaben, aber nichts funktionierte. Zwei von ihnen sind:

Erstens:

$(document).ready(function() { 
     document.addEventListener("deviceready", setOverrideBackbutton, false); 
    }); 

function setOverrideBackbutton() { 
    document.addEventListener("backbutton", backButtonTap, true); 
} 

/** 
* Callback after a backbutton tap on windows platforms. 
* Do nothing. 
*/ 
function backButtonTap() { 
    //Do not remove 
    alert("back button clicked."); 
} 

Zweitens:

link: function(scope, element, attrs) { 
    element.on('click', function() { 
     $window.history.back(); 
    }); 
} 

Aber ich bin nicht in der Lage, dies zu tun. Ich schätze es, weil meine App eine einzelne Seite App ist. was soll ich machen.

Vielen Dank im Voraus.!

Antwort

3

Methode zum Registrieren eines Ereignisses onBackKeyDown(); aussehen würde wie folgt

function onBackKeyDown() { 
    // Back key pressed, do something here 
} 

Standardmethode in den Backbutton-Ereignis Haken:

if (device.platform == "windows") { 
    // Get the back button working in WP8.1 
    WinJS.Application.onbackclick = function() { 
     onBackKeyDown(); 
     return true; // This line is important, without it the app closes. 
    } 
} 
else { 
    document.addEventListener("backbutton", onBackKeyDown, false); 
} 

Nun fügen Sie einfach eine Funktion, den onBackKeyDown Ereignis zu behandeln und das ist es

<!DOCTYPE html> 
<html> 
    <head> 
    <title>Cordova Back Button Example</title> 

    <script type="text/javascript" charset="utf-8" src="cordova-2.5.0.js"></script> 
    <script type="text/javascript" charset="utf-8"> 

    // Call onDeviceReady when Cordova is loaded. 
    // 
    // At this point, the document has loaded but cordova-2.5.0.js has not. 
    // When Cordova is loaded and talking with the native device, 
    // it will call the event `deviceready`. 
    // 
    function onLoad() { 
     document.addEventListener("deviceready", onDeviceReady, false); 
    } 

    // Cordova is loaded and it is now safe to call Cordova methods 
    // 
    function onDeviceReady() { 
     // Register the event listener 
     document.addEventListener("backbutton", onBackKeyDown, false); 
    } 

    // Handle the back button 
    // 
    function onBackKeyDown() { 
    } 

    </script> 
    </head> 
    <body onload="onLoad()"> 
    </body> 
</html>