【问题标题】:exit cordova Windows 10 application退出科尔多瓦 Windows 10 应用程序
【发布时间】:2017-04-08 16:00:55
【问题描述】:

当我在主页上使用我的 Cordova 应用程序时,我需要向用户显示消息,询问他是否真的要退出应用程序。
如果用户选择“是”,我需要退出应用程序。

为此,我使用以下代码:

document.addEventListener('backbutton', function() {
    $rootScope.back();
    $rootScope.$apply();
}, false);

$rootScope.back = function(execute) {
    var path = $location.$$path;
    if (path !== '/') {
        $location.path(path.split('/').slice(0, -1).join('/'));
    } else {
        $rootScope.exitApp();
    }
}.bind(this);

$rootScope.exitApp = function() {
    $rootScope.$emit('showPopover', {
        message: $rootScope.LABELS.QUIT_APP,
        confirmCallback: function() {
            if (typeof navigator.app !== 'undefined') {
                navigator.app.exitApp();
            }
        },
        cancelCallback: doNothing
    });
};

它适用于 android 和 iOS,但不适用于 Windows 10 应用程序。

在 W10 中,navigator.app 未定义。
我读到我应该暂停应用程序而不是退出它,所以我尝试了这个 windows quirkswritten in cordova doc (https://cordova.apache.org/docs/en/latest/cordova/events/events.html#backbutton) :

$rootScope.exitApp = function() {
    $rootScope.$emit('showPopover', {
        message: $rootScope.LABELS.QUIT_APP,
        confirmCallback: function() {
            if (typeof navigator.app !== 'undefined') {
                navigator.app.exitApp();
            }
            else if (platformUtils.isWindows()) {
                throw new Error('Exit'); // This will suspend the app
            }
        },
        cancelCallback: doNothing
    });
};

throw new Error('Exit') 被调用并在日志中显示错误,但应用并未暂停。

也许是因为我在一个有角度的应用程序中?

有人有想法吗?

【问题讨论】:

    标签: angularjs windows cordova uwp windows-10-universal


    【解决方案1】:

    你也可以这样用:

    if (device.platform.toLowerCase() === "windows") {
        //this closes the app and leaves a message in the windows system eventlog if error object is provided
        MSApp.terminateApp();
    } else {
        navigator.app.exitApp()
    }
    

    【讨论】:

      【解决方案2】:

      问题是由于$emit 用于显示弹出框。
      如果我在弹出回调中抛出错误,则此错误不会返回到 backbutton 事件回调。

      但是,在 windows 平台上,我们不应该显示弹出框来询问用户是否确定要离开应用程序,所以如果我将它添加到“返回”功能,它就可以工作。

      用于 Windows 10 应用的最终代码是:

      document.addEventListener('backbutton', function() {
          $rootScope.back();
          $rootScope.$apply();
      }, false);
      
      $rootScope.back = function(execute) {
          var path = $location.$$path;
          if (path !== '/') {
              $location.path(path.split('/').slice(0, -1).join('/'));
          } else {
              if (window.device.platform.toLowerCase() === 'windows') {
                  throw new Error('Exit'); // This will suspend the app
              } else {
                  $rootScope.exitApp();
              }
          }
      }.bind(this);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-12-13
        • 2017-06-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-08
        • 2017-12-16
        • 1970-01-01
        相关资源
        最近更新 更多