【问题标题】:Installing and using Cordova plugin in Visual Studio在 Visual Studio 中安装和使用 Cordova 插件
【发布时间】:2016-12-02 11:45:09
【问题描述】:

我有一个使用 OnsenUI、Monaca 和 Cordova 构建的跨平台应用程序,并且我使用 Visual Studio 作为我的 IDE。

我在 Visual Studio 中使用 THIS 指南配置了 Monaca,它正在 100% 工作。

即使应用程序关闭等,我也需要我的应用程序在后台运行,为此我通过 Visual Studio 安装了THIS 插件来完成任务。

安装插件,在 Visual Studio 中,我选择我项目的 config.xml 文件,然后选择 -> 插件 -> 自定义 -> Git 并输入插件的 URL,如上。 Visual Studio 安装插件,我收到一条成功消息。我检查了我的项目的文件夹结构,并在 plugins 文件夹中看到了已安装的插件。

我之前以类似的方式安装了其他插件,例如相机,它已按预期工作。

然后我尝试按照使用指南 HERE 使用插件功能,但我不断收到错误 “Uncaught TypeError: Cannot read property 'backgroundMode' of undefined”强>

我也尝试按照一些建议调用如下函数,但我仍然得到同样的错误。

window.cordova.plugins.backgroundMode.setDefaults({ text: 'Doing heavy tasks.' });

我尝试在 controller 中调用该函数,如下所示(缩写示例)

var mainNavigation = angular.module("mainNavigationController", []);
mainNavigation.controller("MainNavigationController", function ($scope, NetworkConnection) {
    var init = function () {
        document.addEventListener("deviceready", onDeviceReady, false);
    };

    init();

    function onDeviceReady() {
        cordova.plugins.backgroundMode.setDefaults({ text: 'Doing heavy tasks.' }); // Not working
        window.cordova.plugins.backgroundMode.setDefaults({ text: 'Doing heavy tasks.' }); // Not working either
    };
});

我也尝试将示例代码添加到我的项目 loader.js 文件中,如下所示(缩写版本),但我再次遇到相同的错误。

// This is an auto-generated code by Monaca JS/CSS Components. Please do not edit by hand.
/*** <Start:monaca-cordova-loader> ***/
/*** <Start:monaca-cordova-loader LoadJs:"components/monaca-cordova-loader/cordova-loader.js"> ***/
(function(){
  function getDeviceObjectForPreview() {
    // Code omitted
  }

/*** <Start:monaca-core-utils> ***/
/*** <Start:monaca-core-utils LoadJs:"components/monaca-core-utils/monaca-core-utils.js"> ***/
/*** Monaca Core Utility Library. This library requires cordova.js ***/
window.monaca = window.monaca || {};
(function() {
    // Code omitted

    monaca.isDeviceReady = monaca.isDeviceReady || false;
    document.addEventListener('deviceready', function(){
        window.monaca.isDeviceReady = true;
        monaca.apiQueue.next();

        cordova.plugins.backgroundMode.setDefaults({ text: 'Doing heavy tasks.' }); // Not working
    }, false);
}

我没有在我的 index.html 文件中导入任何文件,例如

<script src="js/canvasDraw/signatureCapture.js"></script>

为什么会出现未定义的错误?我已经检查过了,cordova.js 也是从 loader.js 加载的。

【问题讨论】:

    标签: javascript git visual-studio cordova plugins


    【解决方案1】:

    确保您使用的是device emulator,而不仅仅是像 Ripple-Nexus 这样的基于浏览器的模拟器。

    浏览器插件无法正常工作。此模式仅适用于调试您的 JS 应用程序和一些常用 API。

    Ripple Simulator 的文档中,您可以找到:

    注意:Ripple 不提供对 Cordova API 或本机设备功能(插件)的完整模拟。它也不模拟特定平台的特定浏览器版本。您可以通过在实际设备或模拟器上进行测试来实现这一点。

    【讨论】:

      猜你喜欢
      • 2015-03-05
      • 1970-01-01
      • 2015-03-07
      • 1970-01-01
      • 2016-02-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多