Using Custom Windows Runtime Components in Non-Metro Applications

不羁的心 提交于 2019-12-09 06:49:32

问题


Imagine a scenario where you have a complex framework of WinRT code that you would like to access from both C++ Metro Apps and C# Desktop Apps.

Is there any way to include WinMD in a non-Metro application?


回答1:


UPDATE: This only works with Windows 8. Microsoft disabled this in Windows 8.1.

Yes, there is. The block when attempting to add them via the reference manager appears to be implemented inside Visual Studio itself. Once added, Visual Studio will treat the referenced file as it would any other WinMD file.

NOTE: THIS WORKAROUND IS NOT SUPPORTED BY MICROSOFT!

To add your custom WinMD file to any Non-Metro application you must first ensure that you are targeting .NET 4.5. This will not work with any prior version of the .NET Framework.

Once you are targeting .NET 4.5 unload the project file and open it for editing. Then, add the following code after the last <PropertyGroup> in the project file:

  <PropertyGroup>
    <TargetPlatformVersion>8.0</TargetPlatformVersion>
  </PropertyGroup>

Next, find the <ItemGroup> that contains your references and add the following:

<Reference Include="{Namespace}">
  <HintPath>{Path to WinMD file}</HintPath>
</Reference>

While I cannot guarantee that no problems will arise from doing this, and given that Microsoft has been VERY clear about the fact that regardless of which Framework Profile you are using the CLR is loading the same Assemblies, I strongly suspect that this will not cause any harm.

A note on Class Libraries. If you include your WinMD into a non-Metro Class Library and then reference that library in a non-Metro application; you will be unable to access any of the custom WinRT types that your Class Library exposes without either referencing the WinMD file in your application or providing wrapper types.



来源:https://stackoverflow.com/questions/12136585/using-custom-windows-runtime-components-in-non-metro-applications

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!