Ionic - 在 Windows Phone 8.1 中禁用硬件后退按钮不起作用

Ionic - disable hardware backbutton in Windows Phone 8.1 not working

我正在使用 IONIC 框架进行开发。我在使用硬件后退按钮时遇到问题。

在 android 中,硬件后退按钮工作得很好,但 windows phone 没有用。 当我使用 windows 上的后退按钮时 phone 最小化应用程序和 returns 设备的主页。

此功能仅适用于 android:

 $ionicPlatform.registerBackButtonAction(function () {
            console.log("Not work in WP");
        }, 100);

求助!!

我找到了解决方案。

In site https://www.hoessl.eu/2014/12/on-using-the-ionic-framework-for-windows-phone-8-1-apps/ have a post calling -> Not fixed yet: Back Button

Not fixed yet: Back Button

With Windows Phone 8.0, listening on the “backbutton” event was pretty simple, just as with android. On WP8.1, this event is not triggered anymore. I haven’t figured out how to enable it yet. Any hint would be appreciated.

But a user commented the solution. follows the passage that worked in my case

后退按钮修复:

设置你的$ionicPlatform.registerBackButtonAction

$ionicPlatform.registerBackButtonAction(function (evt) {
if (evt && evt.type == ‘backclick’) {
$ionicHistory.goBack();
return true;
}
}, 100);

Hookin WinJS 并将其发送到 Ionic :

if(ionic.Platform.isWindowsPhone)
{
WinJS.Application.onbackclick = function (evt) {
$ionicPlatform.hardwareBackButtonClick(evt);
return true;
}
}

轻松修复,花了很长时间才弄明白

Example for placing the code inside app.js

angular.module('starter', ['ionic', 'starter.menu', 'starter.services'])
  .run(function ($ionicPlatform, $ionicHistory, $state, ...) {

$ionicPlatform.registerBackButtonAction(function (evt) {
  if (evt && evt.type == 'backclick') {
    $ionicHistory.goBack();
    return true;
  }
}, 100);

...
$ionicPlatform.ready(function () {
  ...

  if (ionic.Platform.isWindowsPhone()) {
    WinJS.Application.onbackclick = function (evt) {
      if ($state.current.name == 'app.home') {
        //function responsible for exiting the application in Windows phone 8.1
        cordova.exec(null, null, "ExitApp", "execute", []);
      } else {
        $ionicPlatform.hardwareBackButtonClick(evt);
        return true;
      }
    }
  }
});  ...

$stateProvider
  .state('login', {
    url: '/login',
    templateUrl: 'templates/login.html',
    controller: 'LoginCtrl',
    onEnter: function ($state, UserService) {
      console.log("##### - " + UserService.get().isLogged);
      if (UserService.get().isLogged) {
        $state.go("app.home");
      }
    }
  })
  .state('app', {
    url: '/app',
    abstract: true,
    templateUrl: 'templates/menu.html',
    controller: 'MenuCtrl'

  })
  .state('app.secretary', {
    url: '/secretary',
    views: {
      'menuContent': {
        templateUrl: 'templates/secretary/menusecretary.html',
        controller: 'MenuSecretaryCtrl'
      }
    }
  })