问题
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