How to Interop with Windows Runtime in .NET 4.5

和自甴很熟 提交于 2019-12-07 08:49:17

问题


I see this namespace:

System.Runtime.InteropServices.WindowsRuntime

Which provides interop between .NET and WindowsRuntime.

For instance, you can invoke a method in WindowsRuntime when you create a Metro application, as Metro uses WindowsRuntime, like

Windows.System.UserProfile.UserInformation

But when you create a normal .NET console application or WPF application, you can no longer directly reach WindowsRuntime namespaces such as Windows.System

I wonder if it were possible to invoke WindowsRuntime methods by using interop in the above-mentioned namespace.

A lot of thanks in advance!


回答1:


A .NET console mode app or a WPF app are not Metro applications. They can only run on the 'regular' version of Windows. The traditional desktop in the case of Windows 8. So can't interop with WinRT, it isn't loaded in the process. Targeting WinRT requires selecting a specific Metro project template when you start your project.




回答2:


You need to add this to the project file:

<PropertyGroup>
  <TargetPlatformVersion>8.1</TargetPlatformVersion>
</PropertyGroup>

For more details, see How to use specific WinRT API from Desktop apps.



来源:https://stackoverflow.com/questions/10310947/how-to-interop-with-windows-runtime-in-net-4-5

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