如何从托管项目引用本机 WinRT 组件?
据说您可以创建一个可以在 C# 中使用的 C++ WinRT 组件。我有一个简单的测试项目 - 一个 C# XAML 应用程序和一个仅使用基本 WinRT 组件项目模板的 C++ 项目。当我尝试在 C# XAML 项目中添加项目引用时,它说无法添加引用(不知道为什么)。我可以构建本机 WinRT 组件,并生成一个 .winmd 文件,并且可以使用添加引用对话框中的浏览按钮手动添加对此 .winmd 文件的引用。这允许我访问本机库中的方法,但当我构建时,它说它正在使用 Platform.IDisposable,但该类型是在未引用的程序集中定义的。我还没有看到任何会议演示这种情况。
It's been said that you can create a C++ WinRT component that you can consume in C#. I have a simple test project - a C# XAML app, and a C++ project using just the basic WinRT Component project template. When I try to add a project reference in the C# XAML project, it says it cannot add the reference (not sure why). I can build the native WinRT component, and a .winmd file is generated, and I can add a reference to this .winmd file manually using the browse button in the add reference dialog. This allows me to access the methods in the native library, but when I build, it says it is using Platform.IDisposable, but that type is defined in an assembly that isn't referenced. I haven't seen any of the sessions demonstrate this scenario.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
找到答案了!看起来这是一个小故障,但它有一个解决方法:
<一href="http://social.msdn.microsoft.com/Forums/en-US/winappswithnativecode/thread/1900bd21-8693-4127-800a-f35cfd5daac2 ">http://social.msdn.microsoft.com/Forums/en-US/winappswithnativecode/thread/1900bd21-8693-4127-800a-f35cfd5daac2
基本上,首先清理 WinRT 组件项目,然后添加引用,然后手动添加对位于 \VC\bin 中的 platform.winmd 的引用,然后就可以开始了。
Found the answer! Looks like it's a glitch, but it has a work-around:
http://social.msdn.microsoft.com/Forums/en-US/winappswithnativecode/thread/1900bd21-8693-4127-800a-f35cfd5daac2
Basically, first you clean the WinRT component project, then you add the reference, then add a reference manually to platform.winmd located in \VC\bin then you should be good to go.
您引用的 C++ WinRT 程序集可能使用实现 IDisposable 接口的对象,因此您还必须引用包含该接口的 C++ 程序集。
The C++ WinRT assembly you are referencing probably uses objects that are implementing the IDisposable interface so you also have to reference the C++ assembly containing the interface.
所以我遇到了类似的问题,在添加对 Microsoft.VCLibs 的引用后,我无法部署。但我注意到,默认情况下,WinRT 组件被编译为 x86 代码,而 C# Metro 风格应用程序针对任何 cpu,这是我的情况的根本原因。将 C# 应用程序切换到 x86 解决了该问题。
So I run into similar issues, after adding a reference to Microsoft.VCLibs, i wasn't able to deploy. However I noticed, that by default WinRT component is compiled to x86 code whereas the C# Metro style app targets ANY cpu, and this was a root cause in my case. Switching C# app to x86 resolved the problem.