简体   繁体   English

添加“winmd”引用与添加项目引用

[英]Adding a “winmd” reference vs Adding a project reference

I have a Windows Phone 8 project and another project written in C++ ; 我有一个Windows Phone 8项目和另一个用C ++编写的项目; both are in the same solution. 两者都在同一个解决方案中。 The C++ project is a dynamic library used in the WP8 project, and it is configured to produce a Windows Metada file ( .winmd ) on top of the .dll file. C ++项目是WP8项目中使用的动态库,它被配置为在.dll文件之上生成Windows Metada文件( .winmd )。

When adding the C++ project as a Project Reference in the WP8 project, everything works perfectly well. 在WP8项目中将C ++项目添加为项目引用时,一切都运行良好。

However, I'd like to reference directly the binaries instead of the project so I tried referencing the .dll itself but VS2012 would not let me (which I totally understand since the library is unmanaged from what I understand). 但是,我想直接引用二进制文件而不是项目,所以我尝试引用.dll本身,但VS2012不会让我(我完全理解,因为库不受我理解的管理)。 Adding the .winmd file instead works, I mean it compiles without warning/errors ; 添加.winmd文件代替工作,我的意思是它编译没有警告/错误; but it crashes at runtime (I get a TargetInvocationException which is raised because the "actual" code of the C++ library cannot be found). 但它在运行时崩溃(我得到一个TargetInvocationException ,因为无法找到C ++库的“实际”代码而引发)。

When adding the .winmd file, I made sure the .dll file was next to it. 添加.winmd文件时,我确保.dll文件就在它旁边。 Putting both the files in the bin directory of the WP8 project does not work either. 将这两个文件放在WP8项目的bin目录中也不起作用。

I can't find any clues on the internet and I'd be grateful if you could give me some, any hints are welcome! 我在互联网上找不到任何线索,如果你能给我一些,我将不胜感激,欢迎任何提示!

Here is a schema of the trivial architecture I'm trying to set up: 这是我正在尝试设置的简单架构的架构:

建筑

And here is the stacktrace of the exception raised: 这是引发异常的堆栈跟踪:

at System.StubHelpers.StubHelpers.GetWinRTFactoryObject(IntPtr pCPCMD)
   at Sqlite.Sqlite3.sqlite3_open_v2(String filename, Database& db, Int32 flags, String zVfs)
   at SQLite.SQLite3.Open(String filename, Database& db, Int32 flags, IntPtr zVfs)
   at SQLite.SQLiteConnection..ctor(String databasePath, SQLiteOpenFlags openFlags, Boolean storeDateTimeAsTicks)
   at SQLite.SQLiteConnection..ctor(String databasePath, Boolean storeDateTimeAsTicks)
   at WP8ClassLibrary.SomeManager..ctor(String databasePath)
   at WP8App.SomeViewModel..ctor()
   at WP8App.MainPage..ctor()

Tools + Options, Projects and Solutions, Build and Run. 工具+选项,项目和解决方案,构建和运行。 Change the "MSBuild project build output verbosity" setting to Normal. 将“MSBuild项目构建输出详细程度”设置更改为“正常”。

Pay attention to the build output, the messages you see after "XapPackager". 注意构建输出,你在“XapPackager”之后看到的消息。 Which show which files are getting added to the Xap package. 其中显示哪些文件被添加到Xap包中。 Your DLL needs to be in that list. 您的DLL需要在该列表中。 If it is not then your program will fail as described. 如果不是,那么您的程序将如所描述的那样失败。 In which case you'll need to find out why it is getting skipped. 在这种情况下,您需要找出它被跳过的原因。 Start that by checking that the Copy Local property of the .winmd reference is True. 通过检查.winmd引用的Copy Local属性是否为True来启动它。

Well I managed to make it work. 好吧,我设法让它工作。 As the stacktrace in the original post would suggest, I am using SQLite in my application. 正如原帖中的堆栈跟踪所暗示的那样,我在我的应用程序中使用了SQLite。 If I add the winmd file as a reference instead of the C++ project, it looks like some dependencies are not satisfied. 如果我将winmd文件添加为参考而不是C ++项目,则看起来似乎不满足某些依赖项。

Therefore I had to add a reference to SQLite for Windows Phone into the WP8ClassLibrary project. 因此,我必须在WP8ClassLibrary项目中添加对SQLite for Windows PhoneWP8ClassLibrary That was somehow a dumb mistake of mine because this C++ project probably carried this dependency with itself (there are several winmd references in it but I could not guess they would be part of the SQLite for Windows Phone based on their obscure names). 这在某种程度上是我的一个愚蠢的错误,因为这个C ++项目可能带有这种依赖关系(其中有几个winmd引用,但我不能猜测它们将成为基于其模糊名称的SQLite for Windows PhoneSQLite for Windows Phone一部分)。

I just wanted to add my experience to the answer.. 我只是想把我的经验添加到答案中..

If you are trying to add a C++ library directly to C# code (for a Windows Phone 8.1 app in my case), then including the .winmd file enables the compilation but the app crashes on launch. 如果您尝试将C ++库直接添加到C#代码(对于我的案例中的Windows Phone 8.1应用程序),则包含.winmd文件将启用编译,但应用程序在启动时崩溃。 The stack trace only says failed to load C++ dll. 堆栈跟踪仅表示无法加载C ++ DLL。

I had to also add reference to Visual Studio C++ runtime library in the C# application. 我还必须在C#应用程序中添加对Visual Studio C ++运行时库的引用。 I found out about the missing reference by diffing the working .xap (created from a solution that includes both C# and C++ projects) and non working .xap (created from a solution that only includes C# project along with reference to C++ .winmd file) 我通过区分工作.xap(从包含C#和C ++项目的解决方案创建)和非工作.xap(从仅包含C#项目的解决方案创建以及引用C ++ .winmd文件)来发现缺少的引用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM