C++/WinRT 简介Introduction to C++/WinRT

 

 

C++/WinRT 是 Windows 运行时 (WinRT) API 的完全标准新式 C++17 语言投影,以基于标头文件的库的形式实现,旨在为你提供对新式 Windows API 的一流访问。C++/WinRT is an entirely standard modern C++17 language projection for Windows Runtime (WinRT) APIs, implemented as a header-file-based library, and designed to provide you with first-class access to the modern Windows API. 利用 C++/WinRT,你可以采用任何符合标准的 C++17 编译器创作和使用 Windows 运行时 API。With C++/WinRT, you can author and consume Windows Runtime APIs using any standards-compliant C++17 compiler. Windows SDK 包含 C++/WinRT;它已在版本 10.0.17134.0(Windows 10,版本 1803)中引用。The Windows SDK includes C++/WinRT; it was introduced in version 10.0.17134.0 (Windows 10, version 1803).

C++/WinRT 是 Microsoft 推荐的用于替代 C++/CX 语言投影和 Windows 运行时 C++ 模板库 (WRL) 的替代品。C++/WinRT is Microsoft's recommended replacement for the C++/CX language projection, and the Windows Runtime C++ Template Library (WRL). 有关 C++/WinRT 的主题的完整列表介绍了如何与 C++/CX 和 WRL 进行互操作,以及如何通过它们进行移植。The full list of topics about C++/WinRT includes info about both interoperating with, and porting from, C++/CX and WRL.

重要

C++/WinRT 最需要注意的某些重要内容在针对 C++/WinRT 的 SDK 支持针对 C++/WinRT、XAML、VSIX 扩展和 NuGet 包的 Visual Studio 支持部分进行了说明。Some of the most important pieces of C++/WinRT to be aware of are described in the sections SDK support for C++/WinRT and Visual Studio support for C++/WinRT, XAML, the VSIX extension, and the NuGet package.

另请参阅可在哪里找到 C++/WinRT 示例应用?Also see Where can I find C++/WinRT sample apps?.

语言投影Language projections

Windows 运行时基于组件对象模型 (COM) API,根据设计,可通过语言投影 访问它。The Windows Runtime is based on Component Object Model (COM) APIs, and it's designed to be accessed through language projections. 投影将隐藏 COM 详细信息,并为给定语言提供更自然的编程体验。A projection hides the COM details, and provides a more natural programming experience for a given language.

Windows 运行时 API 引用内容中的 C++/WinRT 语言投影The C++/WinRT language projection in the Windows Runtime API reference content

浏览 Windows 运行时 API 时,请单击右上角的“语言” 组合框,然后选择“C++/WinRT” 以查看 API 语法块(当它们在 C++/WinRT 语言投影中出现时)。When you're browsing Windows Runtime APIs, click the Language combo box in the upper right, and select C++/WinRT to view API syntax blocks as they appear in the C++/WinRT language projection.

针对 C++/WinRT、XAML、VSIX 扩展和 NuGet 包的 Visual Studio 支持Visual Studio support for C++/WinRT, XAML, the VSIX extension, and the NuGet package

若要获取 Visual Studio 支持,需要 Visual Studio 2019 或 Visual Studio 2017(至少需要版本 15.6;建议至少使用 15.7)。For Visual Studio support, you'll need Visual Studio 2019 or Visual Studio 2017 (at least version 15.6; we recommend at least 15.7). 从 Visual Studio 安装程序中,安装“通用 Windows 平台开发”工作负荷。From within the Visual Studio Installer, install the Universal Windows Platform development workload. 在“安装详细信息” > “通用 Windows 平台开发”中,选中“C++ (v14x) 通用 Windows 平台工具” 选项(如果尚未这样做)。In Installation Details > Universal Windows Platform development, check the C++ (v14x) Universal Windows Platform tools option(s), if you haven't already done so. 另外,请在 Windows 的“设置” > “更新和安全”& > “面向开发人员”中选择“开发人员模式”选项而非“旁加载应用”选项。 And, in Windows Settings > Update & Security > For developers, choose the Developer mode option rather than the Sideload apps option.

虽然我们建议使用最新版 Visual Studio 和 Windows SDK 进行开发,但如果你使用的 C++/WinRT 版本是 10.0.17763.0(Windows 10 版本 1809)之前的 Windows SDK 随附的,则至少需要在项目中使用 Windows SDK 目标版本 10.0.17134.0(Windows 10 版本 1803)才能使用上述 Windows 命名空间标头。While we recommend that you develop with the latest versions of Visual Studio and the Windows SDK, if you're using a version of C++/WinRT that shipped with the Windows SDK prior to 10.0.17763.0 (Windows 10, version 1809), then, to use the the Windows namespaces headers mentioned above, you'll need a minimum Windows SDK target version in your project of 10.0.17134.0 (Windows 10, version 1803).

需要从 Visual Studio Marketplace 下载并安装最新版 C++/WinRT Visual Studio 扩展 (VSIX)You'll want to download and install the latest version of the C++/WinRT Visual Studio Extension (VSIX) from the Visual Studio Marketplace.

  • VSIX 扩展为你提供 Visual Studio 中的 C++/WinRT 项目和项模板,让你可以开始进行 C++/WinRT 开发。The VSIX extension gives you C++/WinRT project and item templates in Visual Studio, so that you can get started with C++/WinRT development.
  • 另外,它还为你提供 C++/WinRT 投影类型的 Visual Studio 本机调试可视化效果 (natvis);提供与 C# 调试类似的体验。In addition, it gives you Visual Studio native debug visualization (natvis) of C++/WinRT projected types; providing an experience similar to C# debugging. Natvis 对于调试版本是自动的。Natvis is automatic for debug builds. 可以通过定义 WINRT_NATVIS 符号选择加入其发布版本。You can opt into it release builds by defining the symbol WINRT_NATVIS.

用于 C++/WinRT 的 Visual Studio 项目模板在下面的部分介绍。The Visual Studio project templates for C++/WinRT are described in the sections below. 使用已安装的最新版 VSIX 扩展创建新的 C++/WinRT 项目时,新的 C++/WinRT 项目会自动安装 Microsoft.Windows.CppWinRT NuGet 包When you create a new C++/WinRT project with the latest version of the VSIX extension installed, the new C++/WinRT project automatically installs the Microsoft.Windows.CppWinRT NuGet package. Microsoft.Windows.CppWinRT NuGet 包提供 C++/WinRT 生成支持(MSBuild 属性和目标),使项目可以在开发计算机和生成代理(在其上仅安装了 NuGet 包,未安装 VSIX 扩展)之间移植。The Microsoft.Windows.CppWinRT NuGet package provides C++/WinRT build support (MSBuild properties and targets), making your project portable between a development machine and a build agent (on which only the NuGet package, and not the VSIX extension, is installed).

也可通过手动安装 Microsoft.Windows.CppWinRT NuGet 包来转换现有项目。Alternatively, you can convert an existing project by manually installing the Microsoft.Windows.CppWinRT NuGet package. 在安装(或更新到)最新版 VSIX 扩展以后,请在 Visual Studio 中打开现有项目,然后单击“项目”>“管理 NuGet 包...” >“浏览”,在搜索框中键入或粘贴 Microsoft.Windows.CppWinRT ,在搜索结果中选择该项,然后单击“安装”以安装该项目的包。 After installing (or updating to) the latest version of the VSIX extension, open the existing project in Visual Studio, click Project > Manage NuGet Packages... > Browse, type or paste Microsoft.Windows.CppWinRT in the search box, select the item in search results, and then click Install to install the package for that project. 添加该包后,你将获得对该项目的 C++/WinRT MSBuild 支持,包括调用 cppwinrt.exe 工具。Once you've added the package, you'll get C++/WinRT MSBuild support for the project, including invoking the cppwinrt.exe tool.

重要

如果项目是使用 1.0.190128.4 之前的 VSIX 扩展版本创建的(或者这些项目在升级后兼容该扩展版本),请参阅早期版本的 VSIX 扩展If you have projects that were created with (or upgraded to work with) a version of the VSIX extension earlier than 1.0.190128.4, then see Earlier versions of the VSIX extension. 该部分包含有关项目配置的重要信息。在升级项目以使用最新版 VSIX 扩展之前,需了解该信息。That section contains important info about the configuration of your projects, which you'll need to know to upgrade them to use the latest version of the VSIX extension.

  • 由于 C++/WinRT 使用 C++17 标准版中的功能,因此 NuGet 包会在 Visual Studio 中设置项目属性“C/C++” > “语言” > “C++ 语言标准版” > “ISO C++17 标准版(/std:c++17)”。 Because C++/WinRT uses features from the C++17 standard, the NuGet package sets project property C/C++ > Language > C++ Language Standard > ISO C++17 Standard (/std:c++17) in Visual Studio.
  • 它还会添加 /bigobj 编译器选项。It also adds the /bigobj compiler option.
  • 它会添加 /await 编译器选项来启用 co_awaitIt adds the /await compiler option in order to enable co_await.
  • 它会指导 XAML 编译器发出 C++/WinRT codegen。It instructs the XAML compiler to emit C++/WinRT codegen.
  • 你可能还需要设置“合规模式: 是(/permissive-)”,以便进一步约束代码,使之符合标准。You might also want to set Conformance mode: Yes (/permissive-), which further constrains your code to be standards-compliant.
  • 要注意的另一个项目属性是“C/C++” > “常规” > “将警告视为错误”。Another project property to be aware of is C/C++ > General > Treat Warnings As Errors. 请根据喜好将此项设置为“是(/WX)” 或“否(/WX-)” 。Set this to Yes(/WX) or No (/WX-) to taste. 有时候,由 cppwinrt.exe 工具生成的源文件会生成警告,除非向其添加实现。Sometimes, source files generated by the cppwinrt.exe tool generate warnings until you add your implementation to them.

根据上述说明设置系统以后,即可在 Visual Studio 中创建和生成或者打开 C++/WinRT 项目,然后部署它。With your system set up as described above, you'll be able to create and build, or open, a C++/WinRT project in Visual Studio, and deploy it.

从 2.0 版开始,Microsoft.Windows.CppWinRT NuGet 包包含 cppwinrt.exe 工具。As of version 2.0, the Microsoft.Windows.CppWinRT NuGet package includes the cppwinrt.exe tool. 可以将 cppwinrt.exe 工具指向 Windows 运行时元数据 (.winmd) 文件,以便生成基于标头文件且可投影 API 的标准 C++ 库,详见“在 C++/WinRT 代码中使用的元数据”。You can point the cppwinrt.exe tool at a Windows Runtime metadata (.winmd) file to generate a header-file-based standard C++ library that projects the APIs described in the metadata for consumption from C++/WinRT code. Windows 运行时元数据 (.winmd) 文件提供了描述 Windows 运行时 API 图面的规范方法。Windows Runtime metadata (.winmd) files provide a canonical way of describing a Windows Runtime API surface. 在元数据中指向 cppwinrt.exe 后,即可生成一个库,该库可以与在第二方或第三方 Windows 运行时组件中实现的或在你自己的应用程序中实现的任何运行时类配合使用。By pointing cppwinrt.exe at metadata, you can generate a library for use with any runtime class implemented in a second- or third-party Windows Runtime component, or implemented in your own application. 有关详细信息,请参阅通过 C++/WinRT 使用 APIFor more info, see Consume APIs with C++/WinRT.

通过 C++/WinRT,你还可以使用标准 C++ 实现自己的运行时类,而不必求助于 COM 样式的编程。With C++/WinRT, you can also implement your own runtime classes using standard C++, without resorting to COM-style programming. 对于运行时类,你只需在 IDL 文件中描述类型,midl.execppwinrt.exe 将为你生成实现样板源代码文件。For a runtime class, you just describe your types in an IDL file, and midl.exe and cppwinrt.exe generate your implementation boilerplate source code files for you. 也可以从 C++/WinRT 基类派生,这样就可以只实现接口。You can alternatively just implement interfaces by deriving from a C++/WinRT base class. 有关详细信息,请参阅使用 C++/WinRT 创作 APIFor more info, see Author APIs with C++/WinRT.

如需 cppwinrt.exe 工具的自定义选项的列表,请通过项目属性进行设置,详见 Microsoft.Windows.CppWinRT NuGet 包自述文件For a list of customization options for the cppwinrt.exe tool, set via project properties, see the Microsoft.Windows.CppWinRT NuGet package readme.

可以通过项目中安装的 Microsoft.Windows.CppWinRT NuGet 包来标识使用 C++/WinRT MSBuild 支持的项目。You can identify a project that uses the C++/WinRT MSBuild support by the presence of the Microsoft.Windows.CppWinRT NuGet package installed within the project.

下面是由 VSIX 扩展提供的 Visual Studio 项目模板。Here are the Visual Studio project templates provided by the VSIX extension.

空白应用 (C++/WinRT)Blank App (C++/WinRT)

通用 Windows 平台 (UWP) 应用的项目模板,具有 XAML 用户界面。A project template for a Universal Windows Platform (UWP) app that has a XAML user-interface.

Visual Studio 提供了 XAML 编译器支持,用于从每个 XAML 标记文件后面的接口定义语言 (IDL) (.idl) 文件生成实现和标头存根。Visual Studio provides XAML compiler support to generate implementation and header stubs from the Interface Definition Language (IDL) (.idl) file that sits behind each XAML markup file. 在 IDL 文件中,定义需要在应用的 XAML 页面中引用的任何本地运行时类,然后构建项目一次,以便在 Generated Files 中生成实现模板并在 Generated Files\sources 中生成存根类型定义。In an IDL file, define any local runtime classes that you want to reference in your app's XAML pages, and then build the project once to generate implementation templates in Generated Files, and stub type definitions in Generated Files\sources. 然后使用这些存根类型定义作为参考来实现本地运行时类。Then use those stub type definitions for reference to implement your local runtime classes. 请参阅将运行时类重构到 Midl 文件 (.idl) 中See Factoring runtime classes into Midl files (.idl).

用于 C++/WinRT 的 Visual Studio 2019 中的 XAML 设计图面支持与 C# 类似。The XAML design surface support in Visual Studio 2019 for C++/WinRT is close to parity with C#. 在 Visual Studio 2019 中,可以使用“属性”窗口的“事件”选项卡 在 C++/WinRT 项目中添加事件处理程序。In Visual Studio 2019, you can use the Events tab of the Properties window to add event handlers within a C++/WinRT project. 也可将事件处理程序手动添加到代码中—有关详细信息,请参阅在 C++/WinRT 中使用委托处理事件You can also add event handlers to your code manually—see Handle events by using delegates in C++/WinRT for more info.

核心应用 (C++/WinRT)Core App (C++/WinRT)

通用 Windows 平台 (UWP) 应用的项目模板,不使用 XAML。A project template for a Universal Windows Platform (UWP) app that doesn't use XAML.

相反,它使用 Windows.ApplicationModel.Core 命名空间的 C++/WinRT Windows 命名空间标头。Instead, it uses the C++/WinRT Windows namespace header for the Windows.ApplicationModel.Core namespace. 构建并运行后,单击空白空间以添加彩色方块;然后单击彩色方块以拖动它。After building and running, click on an empty space to add a colored square; then click on a colored square to drag it.

Windows 控制台应用程序 (C++/WinRT)Windows Console Application (C++/WinRT)

适用于 Windows 桌面版的 C++/WinRT 客户端应用程序的项目模板,具有控制台用户界面。A project template for a C++/WinRT client application for Windows Desktop, with a console user-interface.

Windows 桌面应用程序 (C++/WinRT)Windows Desktop Application (C++/WinRT)

适用于 Windows 桌面版的 C++/WinRT 客户端应用程序的项目模板,可在 Win32 MessageBox 中显示 Windows 运行时 Windows.Foundation.UriA project template for a C++/WinRT client application for Windows Desktop, which displays a Windows Runtime Windows.Foundation.Uri inside a Win32 MessageBox.

Windows 运行时组件 (C++/WinRT)Windows Runtime Component (C++/WinRT)

组件的项目模板;通常可以在通用 Windows 平台 (UWP) 中使用。A project template for a component; typically for consumption from a Universal Windows Platform (UWP).

此模板演示了 midl.exe > cppwinrt.exe 工具链:首先从 IDL 生成 Windows 运行时元数据 (.winmd),然后从 Windows 运行时元数据生成实现和标头存根。This template demonstrates the midl.exe > cppwinrt.exe toolchain, where Windows Runtime metadata (.winmd) is generated from IDL, and then implementation and header stubs are generated from the Windows Runtime metadata.

在 IDL 文件中,在组件、组件的默认接口和组件实现的任何其他接口中定义运行时类。In an IDL file, define the runtime classes in your component, their default interface, and any other interfaces they implement. 构建项目一次以生成 module.g.cppmodule.h.cpp,同时生成 Generated Files 中的实现模板和 Generated Files\sources 中的存根类型定义。Build the project once to generate module.g.cpp, module.h.cpp, implementation templates in Generated Files, and stub type definitions in Generated Files\sources. 然后使用这些存根类型定义作为参考,在组件中实现运行时类。Then use those the stub type definitions for reference to implement the runtime classes in your component. 请参阅将运行时类重构到 Midl 文件 (.idl) 中See Factoring runtime classes into Midl files (.idl).

将生成的 Windows 运行时组件二进制文件及其 .winmd 与使用它们的 UWP 应用绑定。Bundle the built Windows Runtime component binary and its .winmd with the UWP app consuming them.

早期版本的 VSIX 扩展Earlier versions of the VSIX extension

建议安装(或更新到)最新版 VSIX 扩展We recommend that you install (or update to) the latest version of the VSIX extension. 默认情况下,它已配置为自行更新。It is configured to update itself by default. 如果你这样做,并且项目是使用 1.0.190128.4 之前的 VSIX 扩展版本创建的,则请参阅此部分,因为其中包含重要信息,介绍了如何升级这些项目,使之兼容新版本。If you do that, and you have projects that were created with a version of the VSIX extension earlier than 1.0.190128.4, then this section contains important info about upgrading those projects to work with the new version. 当然,如果你不进行更新,此部分的内容仍然对你有用。If you don't update, then you'll still find the info in this section useful.

就支持的 Windows SDK 和 Visual Studio 版本以及 Visual Studio 配置来说,上面的针对 C++/WinRT、XAML、VSIX 扩展和 NuGet 包的 Visual Studio 支持部分的信息适用于早期版本的 VSIX 扩展。In terms of supported Windows SDK and Visual Studio versions, and Visual Studio configuration, the info in the Visual Studio support for C++/WinRT, XAML, the VSIX extension, and the NuGet package section above applies to earlier versions of the VSIX extension. 下面的内容介绍了在使用早期版本创建项目(或将项目升级,使之兼容早期版本)的情况下,在行为和配置方面存在的重要差异。The info below describes important differences regarding the behavior and configuration of projects created with (or upgraded to work with) earlier versions.

在 1.0.181002.2 之前创建Created earlier than 1.0.181002.2

如果项目是使用 1.0.181002.2 之前的 VSIX 扩展版本创建的,则 C++/WinRT 生成支持已内置到该 VSIX 扩展版本中。If your project was created with a version of the VSIX extension earlier than 1.0.181002.2, then C++/WinRT build support was built into that version of the VSIX extension. 项目在 .vcxproj 文件中设置 <CppWinRTEnabled>true</CppWinRTEnabled> 属性。Your project has the <CppWinRTEnabled>true</CppWinRTEnabled> property set in the .vcxproj file.

<Project ...>
    <PropertyGroup Label="Globals">
        <CppWinRTEnabled>true</CppWinRTEnabled>
...

可通过手动安装 Microsoft.Windows.CppWinRT NuGet 包来升级项目。You can upgrade your project by manually installing the Microsoft.Windows.CppWinRT NuGet package. 在安装(或升级到)最新版 VSIX 扩展以后,请在 Visual Studio 中打开项目,然后单击“项目”>“管理 NuGet 包...” >“浏览”,在搜索框中键入或粘贴 Microsoft.Windows.CppWinRT,在搜索结果中选择该项,然后单击“安装”以安装项目的包。 After installing (or upgrading to) the latest version of the VSIX extension, open your project in Visual Studio, click Project > Manage NuGet Packages... > Browse, type or paste Microsoft.Windows.CppWinRT in the search box, select the item in search results, and then click Install to install the package for your project.

使用 1.0.181002.2 和 1.0.190128.3 之间的版本创建(或升级到该版本)Created with (or upgraded to) between 1.0.181002.2 and 1.0.190128.3

如果项目是使用 1.0.181002.2 和 1.0.190128.3 之间的 VSIX 扩展版本(含 1.0.181002.2 和 1.0.190128.3)创建的,则 Microsoft.Windows.CppWinRT NuGet 包已通过项目模板自动安装到项目中。If your project was created with a version of the VSIX extension between 1.0.181002.2 and 1.0.190128.3, inclusive, then the Microsoft.Windows.CppWinRT NuGet package was installed in the project automatically by the project template. 另外,你可能已经升级了旧项目,因此可以使用此范围中的 VSIX 扩展版本。You might also have upgraded an older project to use a version of the VSIX extension in this range. 如果你这样做了,那么,由于生成支持也存在于此范围的 VSIX 扩展版本中,升级的项目可能安装了 Microsoft.Windows.CppWinRT NuGet 包,也可能没有。If you did, then—since build support was also still present in versions of the VSIX extension in this range—your upgraded project may or may not have the Microsoft.Windows.CppWinRT NuGet package installed.

若要升级项目,请按上一部分的说明操作,确保项目安装了 Microsoft.Windows.CppWinRT NuGet 包。To upgrade your project, follow the instructions in the previous section and ensure that your project does have the Microsoft.Windows.CppWinRT NuGet package installed.

无效的升级配置Invalid upgrade configurations

使用最新版 VSIX 扩展时,项目在尚未安装 Microsoft.Windows.CppWinRT NuGet 包的情况下不能有 <CppWinRTEnabled>true</CppWinRTEnabled> 属性。With the latest version of the VSIX extension, it's not valid for a project to have the <CppWinRTEnabled>true</CppWinRTEnabled> property if it doesn't also have the Microsoft.Windows.CppWinRT NuGet package installed. 使用此配置的项目会出现一个生成错误消息:“C++/WinRT VSIX 不再提供项目生成支持。A project with this configuration produces the build error message, "The C++/WinRT VSIX no longer provides project build support. 请添加对 Microsoft.Windows.CppWinRT Nuget 包的项目引用。”Please add a project reference to the Microsoft.Windows.CppWinRT Nuget package."

如上所述,现在需要在 C++/WinRT 项目中安装 NuGet 包。As mentioned above, a C++/WinRT project now needs to have the NuGet package installed in it.

由于 <CppWinRTEnabled> 元素现已过时,你可以选择编辑 .vcxproj,删除该元素。Since the <CppWinRTEnabled> element is now obsolete, you can optionally edit your .vcxproj, and delete the element. 严格说来这不是必需的,只是一种选择。It's not strictly necessary, but it's an option.

另外,如果 .vcxproj 包含 <RequiredBundles>$(RequiredBundles);Microsoft.Windows.CppWinRT</RequiredBundles>,则可将其删除,这样就可以在不需安装 C++/WinRT VSIX 扩展的情况下进行构建。Also, if your .vcxproj contains <RequiredBundles>$(RequiredBundles);Microsoft.Windows.CppWinRT</RequiredBundles>, then you can remove it so that you can build without requiring the C++/WinRT VSIX extension to be installed.

对 C++/WinRT 的 SDK 支持SDK support for C++/WinRT

从版本 10.0.17134.0(Windows 10 版本 1803)起,Windows SDK 包含基于标头文件的标准 C++ 库,因此可以使用第一方 Windows API(Windows 命名空间中的 Windows 运行时 API),但现在其存在只是出于兼容性原因。Although it is now present only for compatibility reasons, as of version 10.0.17134.0 (Windows 10, version 1803), the Windows SDK contains a header-file-based standard C++ library for consuming first-party Windows APIs (Windows Runtime APIs in Windows namespaces). 这些标头位于文件夹 %WindowsSdkDir%Include<WindowsTargetPlatformVersion>\cppwinrt\winrt 中。Those headers are inside the folder %WindowsSdkDir%Include<WindowsTargetPlatformVersion>\cppwinrt\winrt. 从 Windows SDK 版本 10.0.17763.0(Windows 10 版本 1809)开始,将会在项目的 $(GeneratedFilesDir) 文件夹中生成这些标头。As of the Windows SDK version 10.0.17763.0 (Windows 10, version 1809), these headers are generated for you inside your project's $(GeneratedFilesDir) folder.

Windows SDK 也附带 cppwinrt.exe 工具,这同样是出于兼容性原因。Again for compatibility, the Windows SDK also comes with the cppwinrt.exe tool. 但是,建议你改为安装并使用 Microsoft.Windows.CppWinRT NuGet 包随附的最新版 cppwinrt.exeHowever, we recommend that you instead install and use the most recent version of cppwinrt.exe, which is included with the Microsoft.Windows.CppWinRT NuGet package. 该包和 cppwinrt.exe 在上述部分进行了介绍。That package, and cppwinrt.exe, are described in the sections above.

C++/WinRT 投影中的自定义类型Custom types in the C++/WinRT projection

在 C++/WinRT 编程中,你可以使用标准 C++ 语言功能以及标准 C++ 数据类型和 C++/WinRT—包括一些 C++ 标准库数据类型。In your C++/WinRT programming, you can use standard C++ language features and Standard C++ data types and C++/WinRT—including some C++ Standard Library data types. 但你还会在投影中发现一些自定义数据类型,并且可以选择使用它们。But you'll also become aware of some custom data types in the projection, and you can choose to use them. 例如,我们使用 C++/WinRT 入门中快速入门代码示例中的 winrt::hstringFor example, we use winrt::hstring in the quick-start code example in Get started with C++/WinRT.

winrt::com_array 是你可能在某个时间使用的另一个类型。winrt::com_array is another type that you're likely to use at some point. 但你不太可能直接使用 winrt::array_view 之类的类型。But you're less likely to directly use a type such as winrt::array_view. 或者,你可以选择不使用它,这样就可以在等效类型出现在 C++ 标准库中时不用更改任何代码。Or you may choose not to use it so that you won't have any code to change if and when an equivalent type appears in the C++ Standard Library.

警告

如果仔细研究 C++/WinRT Windows 命名空间标头,你可能也会发现一些类型。There are also types that you might see if you closely study the C++/WinRT Windows namespace headers. 一个示例是 winrt::param::hstring,另外还有一些示例。An example is winrt::param::hstring, but there are collection examples too. 这些类型只是为了优化输入参数的绑定,它们实现了显著的性能改进,并使得大多数调用模式“只适用于”相关的标准 C++ 类型和容器。These exist solely to optimize the binding of input parameters, and they yield big performance improvements and make most calling patterns "just work" for related standard C++ types and containers. 在增加最大价值的情况下,这些类型只被投影使用过。These types are only ever used by the projection in cases where they add most value. 它们被高度优化且不用于一般用途;请不要尝试自行使用。They're highly optimized and they're not for general use; don't be tempted to use them yourself. 也不应该使用来自 winrt::impl 命名空间的任何内容,因为这些属于实现类型,因此可能更改。Nor should you use anything from the winrt::impl namespace, since those are implementation types, and therefore subject to change. 你应该继续使用标准类型,或来自 winrt 命名空间的类型。You should continue to use standard types, or types from the winrt namespace.

另请参阅将参数传递到 ABI 边界Also see Passing parameters into the ABI boundary.

重要的 APIImportant APIs