admin管理员组

文章数量:1631139

I was trying to access some of the sensors that are built into this Intel Ultrabook that runs Windows 8. However, while there's support for Location Sensors built into the .NET 4 libraries on Windows 7 and up, I want to access the complete Sensor and Location Platform that is built into Windows 8 itself. Those APIs are available via COM and I could call them via COM, but calling them via the WinRT layer is so much nicer. Plus, this is kind of why WinRT exists.

我试图访问运行Windows 8的此Intel Ultrabook内置的一些传感器。但是,尽管Windows 7及更高版本支持内置于.NET 4库中的位置传感器,但我想访问完整的Sensor和Windows 8本身内置的定位平台。 这些API可通过COM获得,我可以通过COM进行调用,但是通过WinRT层调用它们会更好。 另外,这就是WinRT存在的原因。

This got me thinking about WinRT and what it means. I did a podcast a few months ago that really cleared things up but I've always found all the various diagrams that attempted to explain how things fit together to be WAY TOO COMPLEX.

这让我开始思考WinRT及其含义。 几个月前我做了一个播客,确实清除了一切,但是我总是找到所有各种图,试图解释东西如何组合在一起变得过于复杂。

DISCLAIMER: All diagrams are, by their nature, oversimplifications. I work on Web Stuff, not Windows Stuff, so this is all my opinion and conjecture, done on my own time. I'm not in the Windows org, I'm just a dude trying to write an app for babies.

免责声明:就其性质而言,所有图表均过于简化。 我从事的是Web Stuff,而不是Windows Stuff,因此,这是我的全部观点和推测,都是根据自己的时间完成的。 我不在Windows组织中,我只是一个试图为婴儿编写应用程序的家伙

I figure it can't be as complicated as all these diagrams. I drew this to help myself understand.

我认为它不可能像所有这些图一样复杂。 我画这是为了帮助自己理解。

Just like the C Language has the C Runtime that provides a bunch of supporting functions and defines a calling convention for them, so the Windows Runtime (WinRT) does for Windows and its languages. These APIs and runtime includes metadata about calling conventions that make WinRT APIs easier to call than COM.

就像C语言具有C运行时一样,C运行时提供了很多支持功能并定义了它们的调用约定,因此Windows运行时(WinRT)也适用于Windows及其语言。 这些API和运行时包括有关调用约定的元数据,这些约定使WinRT API的调用比COM更容易。

See how in the diagram I can call any API from the .NET CLR? In the case of the Sensors APIs I want to call, while they are ultimately Win32 APIs or COM APIs, I would like to call them using the highest level calling convention available and that's the very friendly Windows RT ones.

看看在图中如何从.NET CLR调用任何API? 对于我要调用的Sensors API,虽然它们最终是Win32 API或COM API,但我想使用可用的最高级别调用约定来调用它们,这就是非常友好的Windows RT。

从C#桌面应用程序调用WinRT API (Calling WinRT APIs from C# Desktop Applications)

I like to test things using small Console Apps, but those aren't "Windows Store Applications," so am I allowed to call WinRT APIs from my Desktop or Console application?

我喜欢使用小型控制台应用程序进行测试,但不是“ Windows Store应用程序”,因此可以从台式机或控制台应用程序调用WinRT API吗?

Sure. There's actually a section of the MSDN Documentation that lists out all the WinRT APIs for Windows 8 that are able to be called from the Desktop. I can specifically check the LightSensor class itself within the documentation and make sure it's allowed to be called from Desktop applications.

当然。 MSDN文档中实际上有一部分列出了所有Windows 8的WinRT API,这些API可以从Desktop调用。 我可以在文档中专门检查LightSensor类本身,并确保允许从桌面应用程序调用它。

There isn't super-clear but there IS documentation on how to add WinRT references to non-Windows Store applications.

尚不十分清楚,但是有关于如何将WinRT引用添加到非Windows Store应用程序的文档。

从桌面应用程序添加对WinRT的引用 (Adding a Reference to WinRT from a Desktop App)

The docs say, somewhat obscurely:

文档说得有些晦涩:

In the desktop projects, the Core tab doesn’t appear by default. The user can choose to code against the Windows Runtime by opening the shortcut menu for the project node, choosing Unload Project, adding the following snippet, opening the shortcut menu for the project node again, and then choosing Reload Project. Now, when the user invokes the Reference Manager dialog box from the project, the Core tab will appear.

在桌面项目中,默认情况下不显示“核心”选项卡。 通过打开项目节点的快捷菜单,选择“卸载项目”,添加以下代码段,再次打开项目节点的快捷菜单,然后选择“重新加载项目”,用户可以选择针对Windows运行时进行编码。 现在,当用户从项目中调用“参考管理器”对话框时,将显示“核心”选项卡。

   <propertygroup>
    <targetplatformversion>8.0</targetplatformversion>
  </propertygroup>

I'll make a .NET 4.5 C# Console Application. I'll edit the .csproj and add the TargetPlatformVersion line. I'll select Add Reference from the context menu on the References node of Solution Explorer.

我将制作一个.NET 4.5 C#控制台应用程序。 我将编辑.csproj并添加TargetPlatformVersion行。 我将从“解决方案资源管理器”的“引用”节点上的上下文菜单中选择“添加引用”。

I'll add a little code to check the status of the Light Sensor on my laptop:

我将添加一些代码来检查笔记本电脑上的光传感器的状态:

LightSensor light = LightSensor.GetDefault();
if (light != null)
{
uint minReportInterval = light.MinimumReportInterval;
uint reportInterval = minReportInterval > 16 ? minReportInterval : 16;
light.ReportInterval = reportInterval;

light.ReadingChanged += light_ReadingChanged; //event hander
}

However, when I compile the app, I get an error on the line where I'm trying to hook up an event handler. The "+=" language sugar for adding a multicast delegate isn't working.

但是,当我编译应用程序时,在尝试连接事件处理程序的那一行上出现错误。 用于添加多播代理的“ + =”语言糖无法正常工作。

Error    1    Property, indexer, or event 
'Windows.Devices.Sensors.LightSensor.ReadingChanged'
is not supported by the language; try directly calling accessor
methods 'Windows.Devices.Sensors.LightSensor.add_ReadingChanged
(Windows.Foundation.TypedEventHandler
Windows.Devices.Sensors.LightSensorReadingChangedEventArgs>)'
or 'Windows.Devices.Sensors.LightSensor.remove_ReadingChanged
(System.Runtime.InteropServices.WindowsRuntime.EventRegistrationToken)'

To fix this and get the appropriate assemblies loaded within my application support calling WinRT from my Desktop Application I need to add a reference to System.Runtime and System.Runtime.InteropServices.WindowsRuntime.dll. It's in C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETCore\v4.5 on my system.

要解决此问题并在从我的桌面应用程序调用WinRT的应用程序支持中加载适当的程序集,我需要添加对System.Runtime和System.Runtime.InteropServices.WindowsRuntime.dll的引用。 它在我系统上的C:\ Program Files(x86)\ Reference Assemblies \ Microsoft \ Framework \ .NETCore \ v4.5中。

Now my app compiles. I'll even change out the delegate and make it a Anders lambda because that's fancy.

现在,我的应用程序开始编译。 我什至会换成代表,并使其成为 Anders lambda,因为这很不错。

light.ReadingChanged += (s, a) =>
{
Console.WriteLine(String.Format("There was light! {0}", a.Reading.IlluminanceInLux));
};

Now I can run my little console app, sense some light and check it out in action. Here's a screenshot showing the results of me shining a light at my laptop. You can see the Ambient LightSensor picks it up and outputs to the Console.

现在,我可以运行我的小型控制台应用程序,感知一些光线并进行实际检查。 这是一个屏幕截图,显示了我向笔记本电脑照射光的结果。 您可以看到Ambient LightSensor将其拾取并输出到控制台。

While the tooling to make non-Windows Store applications call Windows RT applications is a little manual within Visual Studio right now, the underlying ability and runtime have work very nicely for me. Hopefully these few manual setups will turn into a checkbox at some point.

虽然使非Windows应用商店应用程序调用Windows RT应用程序的工具目前在Visual Studio中是一本小手册,但基本功能和运行时对我来说非常好。 希望这些手动设置会在某个时候变成一个复选框。

It's also nice to see the MSDN documentation includes the details about which APIs actually can be called from the Desktop and which can be called from Windows Store apps.

也很高兴看到MSDN文档包含有关哪些API实际上可以从桌面调用以及哪些可以从Windows Store应用程序调用的详细信息。

This week's sponsor: Your Idea. Your App. 30 Days. Begin your 30-day journey to creating a Windows Store app or game for Windows 8 or Windows Phone today.

本周赞助商:您的想法。 您的应用。 30天。 立即开始为期30天的旅程,立即为Windows 8或Windows Phone创建Windows应用商店应用或游戏。

翻译自: https://www.hanselman/blog/how-to-call-winrt-apis-in-windows-8-from-c-desktop-applications-winrt-diagram

本文标签: 应用程序桌面如何在APIWindows