您自己确实很难发现这一点,但实际上您可以将这些库绑定到您的 MSIX。就我而言,我只是使用制造商提供的 Dart FFI 和 DLL 为标签打印机制作了一个包,这就是我的做法。
您需要将这些 DLL 添加到您的assets
设置为pubspec.yaml
从你的包裹中。这是我的情况:
[...]
flutter:
[...]
assets:
- assets/WinPort.dll
- assets/Winppla.dll
- assets/Winpplb.dll
- assets/Winpplz.dll
通过此设置,您将把 DLL 文件嵌入到最终的 MSIX 中,但这是简单的部分。现在您必须确保在代码中正确加载这些文件。
根据我自己的测试,我仍然处理两种开发和测试代码的方法,第一种是当我通过以下方式在我的机器中运行项目时flutter run
我必须设置 current.path 的目标,当我完成它并开始构建部署时,我将其更改为resolvedExecutable.parent.path。哪里是你需要做的事情。
在开发环境中加载 DLL(flutter run
):
final String _packageAssetsDirPath = normalize(join(Directory.current.path,'assets'));
在生产环境中(从安装的 .exe 或 MSIX 运行):
final String _assetsPackageDir = normalize(
join('data', 'flutter_assets', 'packages', 'YOUR_PACKAGE_NAME', 'assets'));
final String _exeDirPath = File(Platform.resolvedExecutable).parent.path;
final String _packageAssetsDirPath =
normalize(join(_exeDirPath, _assetsPackageDir));
在这个 var 调用之后_packageAssetsDirPath
加载 DLL 会很容易,现在调用 DynamicLibrary 构造函数:
// Path for DLL file
final String _libDllSourceFullPath =
normalize(join(_packageAssetsDirPath, 'Winppla.dll'));
// Target for copy, place DLL in same place the .exe you are running
final String _libDllDestFullPath =
normalize(join(_packageAssetsDirPath, 'YOUROWN.dll'));
// Try to copy for running exe path
File(_libDllSourceFullPath).copySync(_libDllDestFullPath);
// With this copy, would be simple to load, and if it fails, try in full path
// LOAD DLL
try {
String _packageAssetsDirPath =
normalize(join(Directory.current.path, 'assets'));
String _printerLibraryPath =
normalize(join(_packageAssetsDirPath, 'Winppla.dll'));
DynamicLibrary _library = DynamicLibrary.open(_printerLibraryPath);
return _library;
} catch (e) {
try {
DynamicLibrary _library = DynamicLibrary.open('Winppla.dll');
return _library;
} catch (e) {
// Avoing errors creating a fake DLL, but you could deal with an exception
return DynamicLibrary.process();
}
}
此时您可以加载 DLL 并使用它,您可以在以下位置查看我的包完整代码https://github.com/saviobatista/argox_printer https://github.com/saviobatista/argox_printer检查lib/src/ppla.dart
在函数中_setupDll()
你会看到加载。