在基于 C++ 的 UWP 应用程序中缺少 ShutdownManager class?

Missing ShutdownManager class in C++ based UWP app?

我目前正在研究 Windows IoT Core,我正在尝试为 Raspberry Pi 3b 开发一个基于 C++ 的 UWP 应用程序。 我想从应用程序内部 reboot/shutdown 树莓派,但我不确定该怎么做。

UWP api 参考列出了对应于 C++ Windows::System 的 C# Windows.System。但是似乎并不是所有的classes都可以在C++中使用。 UWP samples (C#) 利用 ShutdownManager class 进行重启和关机,但它不存在于 C++ 命名空间中。现在我不确定应该如何使用 API 来实现此功能。是否有任何其他可能性可以为 C++ 解决此问题?

解法: 我错过了 UWP 的 Windows 物联网扩展。这些必须通过右键单击项目手动添加到 Visual Studio,然后添加 -> 参考 -> 通用 Windows -> 扩展。现在我可以访问命名空间并能够重启和关闭我的设备。

But it seems that not all classes are available in C++.

是说你在构建C++ UWP项目的时候无法构建成功,还是在设备上运行应用程序出现异常?

ShutdownManager API 在 C++ 中可用,确实包含在 Windows::System 命名空间中,请参阅此 document。请注意:

This API requires the use of the IoT systemManagement capability, and the inclusion of iot in the IgnorableNamespaces list. Users can add the following to their Package.appmanifest: , and add iot to their existing list of IgnorableNamespaces.

以下代码在我的设备上运行良好。

TimeSpan ts;
ts.Duration = 1000;
ShutdownManager::BeginShutdown(ShutdownKind::Shutdown, ts);