Windows 8 Phone 的通用自定义插件找不到入口 js 对象
generic custom plugin for Windows 8 Phone can't find entry js object
我需要创建一个自定义的 cordova 插件并将其安装到基于 cordova 的 Windows 8 phone 应用程序中。目前,应用程序中的按钮点击处理程序无法看到调用 cordova.exec
的 js 对象
具有调用 cordova.exe 函数的对象称为 dlScanner
它有一个函数 scanBarcode 调用 cordova.exec
点击处理程序是这样的:
$(document).ready(function () {
$('#cmdOne').click(function () {
dlScanner.scanBarcode(
function (results) {
alert(results);
}), function (err) {
alert(err);
}, 'lowercaseworld'
});
});
错误信息是这样的:
TypeError: Cannot read property 'scanBarcode' of undefined
上下文:我使用了 Microsoft 插件生成器 PluginGenerator,发现 here。然后,我在 Visual Studio 社区 2015 中的应用程序 config.xml 文件上使用视图设计器将该插件安装到 VS 创建的通用 cordova Windows 8 phone 应用程序中早点给我。
该插件安装过程将其放入应用程序的 config.xml 文件中
<vs:plugin name="com.contoso.dlScanner" version="0.1.0" src="C:\Users\TestAndDemo\dlScanner" />
在 dlScanner 目录(上面提到的)中有 src 和 www 目录以及一个 plugin.xml 文件
在那个 plugin.xml 文件中是这样的:
<js-module src="www/dlScanner.js" name="dlScanner">
<clobbers target="dlScanner" />
</js-module>
<!-- wp8 -->
<platform name="wp8">
<config-file target="config.xml" parent="/*">
<feature name="dlScanner">
<param name="wp-package" value="dlScanner"/>
</feature>
</config-file>
<source-file src="src/wp/dlScanner.cs" />
www/dlScanner.js 文件包含:
var dlScanner = {
scanBarcode: function (successCallback, errorCallback, strInput) {
cordova.exec(successCallback, errorCallback, "dlScanner", "scanBarcode", [strInput]);
}
}
module.exports = dlScanner;
我需要做什么才能让点击处理程序看到 dlScanner 对象?
谢谢
尝试在 Cordova 初始化并发出 deviceready
事件后调用插件。在此处查看有关此事件的更多信息:http://docs.phonegap.com/en/3.5.0/cordova_events_events.md.html#deviceready.
有几个问题:
VS 中的视图设计器、自动插件安装程序从未在我的 index.html 文件中添加对 dlScanner.js 文件的引用。所以我不得不将该文件移动到脚本目录并将其添加到 index.html
<script src="scripts/index.js"></script>
这使得 cordova.exec 运行 并且我的点击处理程序开始点击 dlScanner 代码。然后我开始收到这个错误
module is undefined
这告诉我(我认为)当 module.exports 行 运行 时 cordova 还没有准备好,所以我把 module.exports 的东西放在 onDeviceReady
function onLoad() {
document.addEventListener("deviceready", onDeviceReady, false);
}
function onDeviceReady() {
module.exports = dlScanner;
}
感谢@Vlad
我需要创建一个自定义的 cordova 插件并将其安装到基于 cordova 的 Windows 8 phone 应用程序中。目前,应用程序中的按钮点击处理程序无法看到调用 cordova.exec
的 js 对象具有调用 cordova.exe 函数的对象称为 dlScanner 它有一个函数 scanBarcode 调用 cordova.exec
点击处理程序是这样的:
$(document).ready(function () {
$('#cmdOne').click(function () {
dlScanner.scanBarcode(
function (results) {
alert(results);
}), function (err) {
alert(err);
}, 'lowercaseworld'
});
});
错误信息是这样的:
TypeError: Cannot read property 'scanBarcode' of undefined
上下文:我使用了 Microsoft 插件生成器 PluginGenerator,发现 here。然后,我在 Visual Studio 社区 2015 中的应用程序 config.xml 文件上使用视图设计器将该插件安装到 VS 创建的通用 cordova Windows 8 phone 应用程序中早点给我。
该插件安装过程将其放入应用程序的 config.xml 文件中
<vs:plugin name="com.contoso.dlScanner" version="0.1.0" src="C:\Users\TestAndDemo\dlScanner" />
在 dlScanner 目录(上面提到的)中有 src 和 www 目录以及一个 plugin.xml 文件
在那个 plugin.xml 文件中是这样的:
<js-module src="www/dlScanner.js" name="dlScanner">
<clobbers target="dlScanner" />
</js-module>
<!-- wp8 -->
<platform name="wp8">
<config-file target="config.xml" parent="/*">
<feature name="dlScanner">
<param name="wp-package" value="dlScanner"/>
</feature>
</config-file>
<source-file src="src/wp/dlScanner.cs" />
www/dlScanner.js 文件包含:
var dlScanner = {
scanBarcode: function (successCallback, errorCallback, strInput) {
cordova.exec(successCallback, errorCallback, "dlScanner", "scanBarcode", [strInput]);
}
}
module.exports = dlScanner;
我需要做什么才能让点击处理程序看到 dlScanner 对象?
谢谢
尝试在 Cordova 初始化并发出 deviceready
事件后调用插件。在此处查看有关此事件的更多信息:http://docs.phonegap.com/en/3.5.0/cordova_events_events.md.html#deviceready.
有几个问题:
VS 中的视图设计器、自动插件安装程序从未在我的 index.html 文件中添加对 dlScanner.js 文件的引用。所以我不得不将该文件移动到脚本目录并将其添加到 index.html
<script src="scripts/index.js"></script>
这使得 cordova.exec 运行 并且我的点击处理程序开始点击 dlScanner 代码。然后我开始收到这个错误
module is undefined
这告诉我(我认为)当 module.exports 行 运行 时 cordova 还没有准备好,所以我把 module.exports 的东西放在 onDeviceReady
function onLoad() {
document.addEventListener("deviceready", onDeviceReady, false);
}
function onDeviceReady() {
module.exports = dlScanner;
}
感谢@Vlad