UWP研究

发布于 2020-11-08  297 次阅读


当前UWP支持C++ 以及 C#进行编写,但UWP当前有两个系统版本的:

  1. Win8.1 VS2013
  2. Win10 VS2015

根据研究,UWP不直接支持Win32的动态库

UWP 应用的特点:

  • 安全:UWP 应用声明其访问哪些设备资源和数据 用户必须对该访问授权。
  • 能够在运行 Windows 10 的所有设备上使用常见的 API。
  • 可以使用设备的特定功能并让 UI 适应不同的设备屏幕尺寸、分辨率和 DPI。
  • 通过运行 Windows 10 的所有设备(或只是你指定的设备)上的 Microsoft Store 提供。 Microsoft Store 提供了多种利你的应用赚钱的方式。
  • 能够在不对计算机构成风险或引起“计算机腐烂”的情况下安装和卸载。
  • 互动:使用动态磁贴、推送通知以及与 Windows 时间线和 Cortana 的“Pick Up Where I Left Off”交互的用户活动吸引用户。
  • 可使用 C#、C++、Visual Basic 和 Javascript 编程。 对于 UI,使用 XAML、HTML 或 DirectX。

安全

UWP 应用在其清单中声明所需的设备能力,如访问麦克风、位置、网络摄像头、USB 设备、文件等。 用户必须在应用被授予能力前确认并授权该访问。

跨所有设备的通用 API 设计面

Windows 10 引入了通用 Windows 平台 (UWP),这在运行 Windows 10 的每台设备上提供了通用的应用平台。 UWP 核心 API 在所有 Windows 设备上是相同的。 如果你的应用只使用核心 API,它将在任何 Windows 10 设备上运行,不论你是定位台式电脑、Xbox、混合现实头戴显示设备还是其他设备。
使用 C++ /WinRT 或 C++ /CX 编写的 UWP 应用可以访问属于 UWP 的 Win32 API。 所有 Windows 10 设备都实现这些 Win32 API。

<DeviceCapability Name="4d36e96c-e325-11ce-bfc1-08002be10318">
</DeviceCapability>  

UWP例程

UWP的例程可在github上找到,网址为 https://github.com/Microsoft/Windows-universal-samples

UWP技术细节

UWP中大部分函数目前提供的都是异步接口,对于异步接口在C#中有await可以将异步
转同步,在C++中目前建议使用Concurrency中的create_taskthen.
C++异步转同步的相关官方文档为:
Asynchronous programming in C++/CX
相关代码为:

#include <ppltasks.h>
using namespace concurrency;
using namespace Windows::Devices::Enumeration;
...
void App::TestAsync()
{
    //Call the *Async method that starts the operation.
    IAsyncOperation<DeviceInformationCollection^>^ deviceOp =
        DeviceInformation::FindAllAsync();

    // Explicit construction. (Not recommended)
    // Pass the IAsyncOperation to a task constructor.
    // task<DeviceInformationCollection^> deviceEnumTask(deviceOp);

    // Recommended:
    auto deviceEnumTask = create_task(deviceOp);

    // Call the task's .then member function, and provide
    // the lambda to be invoked when the async operation completes.
    deviceEnumTask.then( [this] (DeviceInformationCollection^ devices )
    {
        for(int i = 0; i < devices->Size; i++)
        {
            DeviceInformation^ di = devices->GetAt(i);
            // Do something with di...
        }
    }); // end lambda
    // Continue doing work or return...
}

朝闻道,夕死可矣