我使用cordova cmd行创建了一个Windows通用项目。 (创建一个新项目,添加平台windows
)
构建项目并从cmd行运行它。工作正常。
我的要求是创建我自己的自定义插件并通过npm cmd line安装它。
我的项目还需要使用Windows运行时组件访问本机代码。
我在Visual Studio 2015中打开了上面的项目 CordovaApp 。
然后我添加了一个Windows运行时组件,我还添加了Newtonsoft库(link here why I added Newtonsoft manually)。我添加了以下服务类:
public sealed class Service
{
public IAsyncOperation<string> Open(string param)
{
return OpenHelper(param).AsAsyncOperation();
}
private async Task<string> OpenHelper(string param)
{
try
{
JObject jObj = JObject.Parse(JArray.Parse(param)[0].ToString());
string message = jObj["message"].ToString();
return message;
}
catch (Exception ex)
{
return "fail";
}
}
}
我想在通过cmd行安装我的自定义插件时添加其dll或winmd文件。所以我从 bin \ x86 \ MyRuntime.winmd 复制了我的 .winmd 文件并将其放在我的自定义插件文件夹(\ src \ windows \ lib)中
我的自定义plugin.xml如下:
<?xml version="1.0" encoding="UTF-8"?>
<plugin xmlns="http://apache.org/cordova/ns/plugins/1.0"
id="cordova-test-plugin"
version="0.1.0">
<name>Launcher</name>
<description>PhoneGap Plugin</description>
<license>MIT</license>
<keywords>phonegap,launcher</keywords>
<js-module src="www/launcher.js" name="Launcher">
<clobbers target="window.launcher" />
</js-module>
<!-- windows -->
<platform name="windows">
<js-module src="src/windows/Launcher.js" name="LauncherProxy">
<merges target="" />
</js-module>
<framework src="src/windows/lib/MyRuntime.winmd" custom="true" />
</platform>
</plugin>
我的cordova插件中有 launcher.js 文件,其中调用了本机运行时组件,如下所示:
Launcher.js
cordova.commandProxy.add("LauncherProxy", {
open: function(successCallback, errorCallback, pluginParam) {
var service = MyRuntime.Service();
service.open(JSON.stringify(pluginParam)).then(function(data) {
successCallback(data);
});
}
});
require("cordova/exec/proxy").add("Launcher", module.exports);
在我的 index.js 中,我在设备就绪事件上调用此方法:
launcher.openFile(function (message) {
Windows.UI.Popups.MessageDialog(message).showAsync();
console.log(message);
}, function () {
});
仅供参考:我的自定义插件的** \ www **中的launcher.js也有以下代码结构:
cordova.define("cordova-test-plugin.Launcher", function(require, exports, module) {
var objWindowsLauncher = {
openFile: function(successCallback, errorCallback) {
cordova.exec(
successCallback,
errorCallback,
'LauncherProxy',
'open',
[{
"message": "Hello test message"
}]
);
}
}
module.exports = objWindowsLauncher;
});
直到现在它运作良好。当应用运行时,它会弹出Hello test message
。
现在我为SQLite添加了一个新的类库项目。安装sqlite-net(包括来自旧项目的SQLite.cs和SQLiteAsync.cs文件),添加了Newtonsoft.json dll,SQlite for Universal Windows Platform,Visual C ++ 2015 Runtime for Universal Windows Platfrom。
通过右键单击Reference并选择SQLite项目,将此SQLite引用(类库)添加到我之前的Runtime Component项目中。 通过这样做,现在我可以在运行时组件中执行sqlite操作。我构建了这个运行时组件,因此它的新 .winmd 文件是在 bin \ x86 \ Debug 中生成的。我复制了这个winmd文件并将其放在我上面的自定义插件文件夹中。
我删除了该插件并重新添加了插件(cmd行安装),就像上面一样。这一次,项目应该和以前一样工作,但是不起作用。
它会引发异常。
System.IO.FileNotFoundException: Could not load file or assembly 'SQLite, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.
at MyRuntime.Service.<OpenHelper>d__1.MoveNext()
at System.Runtime.CompilerServices.AsyncTaskMethodBuilder 1.Start[TStateMachine](TStateMachine& stateMachine)
另一个案例:
有时,我会通过以上完整过程获得此异常:
Could not load file or assembly 'Newtonsoft.Json, Version=9.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' or one of its dependencies
INFO
所有项目构建配置都在x86下。
Newtonsoft版本是:9.0.1,10.xx
Cordova Windows 10项目,运行时组件和类库具有:10.0.14393.0(目标版本)和10.0.10240.0(最低版本)
Cordova版本:7.0.1 Cordova Windows版本:5.0.0
插件安装并删除cmd:
cordova插件删除cordova-test-plugin
cordova插件添加.. \ plugins \ cordova-test-plugin
我已在onedrive分享了包含自定义插件的完整项目。
P.S。我可以手动添加Runtime组件引用并使其工作,但我想使用cmd行界面,安装插件,从cmd行界面生成和生成包,而无需与Visual Studio交互。
请让我知道你的观点,我记得几个月前我开始工作时,它曾经工作过。 (通过在plugin.xml(<framework src="src/windows/lib/MyRuntime.winmd" custom="true" />
)中定义 .winmd 文件,从cmd行添加引用)。它曾经工作得很好。但最近我遇到了上述问题。
更新1
我试过@Elvis Xia建议。这似乎按预期加载运行时组件。我包括了所有这些:
<framework src="src/windows/lib/SQLite.dll" custom="true"/>
<framework src="src/windows/lib/MyRuntime.winmd" custom="true" />
<framework src="src/windows/lib/Newtonsoft.Json.dll" custom="true"/>
在我的plugin.xml中,并通过cmd行安装插件。创建SQLite数据库时仍然存在一些错误。我遇到system.dllnotfoundexception unable to load dll 'sqlite3'
错误:
答案 0 :(得分:0)
问题是,SQLite.dll
和NewtonJson.dll
也应该作为参考添加到您的插件中。所以你的plugin.xml应该是这样的:
<?xml version="1.0" encoding="UTF-8"?>
<plugin xmlns="http://apache.org/cordova/ns/plugins/1.0"
id="cordova-test-plugin"
version="0.1.0">
<name>Launcher</name>
<description>PhoneGap Plugin</description>
<license>MIT</license>
<keywords>phonegap,launcher</keywords>
<js-module src="www/launcher.js" name="Launcher">
<clobbers target="window.launcher" />
</js-module>
<!-- windows -->
<platform name="windows">
<js-module src="src/windows/Launcher.js" name="LauncherProxy">
<merges target="" />
</js-module>
<!-- <framework src="src/windows/lib/MyRuntime.winmd" custom="true" /> -->
<framework src="src/windows/lib/SQLite.dll" custom="true"/>
<framework src="src/windows/lib/MyRuntime.winmd" custom="true" />
<framework src="src/windows/lib/Newtonsoft.Json.dll" custom="true"/>
</platform>
</plugin>
将SQLite.dll
文件夹下的Newtonsoft.Json.dll
和bin\x86\debug\
复制到您的插件src/windows/lib/
。然后它会正常工作。