|
1 | 1 | ---
|
2 | 2 | description: This topic shows how to author C++/WinRT APIs by using the **winrt::implements** base struct, either directly or indirectly.
|
3 | 3 | title: Author APIs with C++/WinRT
|
4 |
| -ms.date: 09/22/2023 |
| 4 | +ms.date: 09/28/2023 |
5 | 5 | ms.topic: article
|
6 | 6 | keywords: windows 10, uwp, standard, c++, cpp, winrt, projected, projection, implementation, implement, runtime class, activation
|
7 | 7 | ms.localizationpriority: medium
|
@@ -288,7 +288,10 @@ IStringable istringable = winrt::make<MyType>();
|
288 | 288 | > [!NOTE]
|
289 | 289 | > However, if you're referencing your type from your XAML UI, then there will be both an implementation type and a projected type in the same project. In that case, **make** returns an instance of the projected type. For a code example of that scenario, see [XAML controls; bind to a C++/WinRT property](binding-property.md#add-a-property-of-type-bookstoreviewmodel-to-mainpage).
|
290 | 290 |
|
291 |
| -We can only use `istringable` (in the code example above) to call the members of the **IStringable** interface. But a C++/WinRT interface (which is a projected interface) derives from [**winrt::Windows::Foundation::IUnknown**](/uwp/cpp-ref-for-winrt/windows-foundation-iunknown). So, you can call [**IUnknown::as**](/uwp/cpp-ref-for-winrt/windows-foundation-iunknown#iunknownas-function) (or [**IUnknown::try_as**](/uwp/cpp-ref-for-winrt/windows-foundation-iunknown#iunknowntry_as-function)) on it to query for other projected types or interfaces, which you can also either use or return. |
| 291 | +We can use `istringable` (in the code example above) only to call the members of the **IStringable** interface. But a C++/WinRT interface (which is a projected interface) derives from [**winrt::Windows::Foundation::IUnknown**](/uwp/cpp-ref-for-winrt/windows-foundation-iunknown). So, you can call [**IUnknown::as**](/uwp/cpp-ref-for-winrt/windows-foundation-iunknown#iunknownas-function) (or [**IUnknown::try_as**](/uwp/cpp-ref-for-winrt/windows-foundation-iunknown#iunknowntry_as-function)) on it to query for other projected types or interfaces, which you can also either use or return. |
| 292 | + |
| 293 | +> [!TIP] |
| 294 | +> A scenario where you *shouldn't* call **as** or **try_as** is runtime class derivation ("composable classes"). When an implementation type composes another class, don't call **as** or **try_as** in order to perform an unchecked or checked **QueryInterface** of the class being composed. Instead, access the (`this->`) `m_inner` data member, and call **as** or **try_as** on that. For more info, see [Runtime class derivation](#runtime-class-derivation) in this topic. |
292 | 295 |
|
293 | 296 | ```cppwinrt
|
294 | 297 | istringable.ToString();
|
@@ -495,6 +498,9 @@ namespace winrt::MyProject::implementation
|
495 | 498 | }
|
496 | 499 | ```
|
497 | 500 |
|
| 501 | +> [!TIP] |
| 502 | +> When an implementation type composes another class, don't call **as** or **try_as** in order to perform an unchecked or checked **QueryInterface** of the class being composed. Instead, access the (`this->`) `m_inner` data member, and call **as** or **try_as** on that. |
| 503 | +
|
498 | 504 | ## Deriving from a type that has a non-default constructor
|
499 | 505 |
|
500 | 506 | [**ToggleButtonAutomationPeer::ToggleButtonAutomationPeer(ToggleButton)**](/uwp/api/windows.ui.xaml.automation.peers.togglebuttonautomationpeer.-ctor#Windows_UI_Xaml_Automation_Peers_ToggleButtonAutomationPeer__ctor_Windows_UI_Xaml_Controls_Primitives_ToggleButton_) is an example of a non-default constructor. There's no default constructor so, to construct a **ToggleButtonAutomationPeer**, you need to pass an *owner*. Consequently, if you derive from **ToggleButtonAutomationPeer**, then you need to provide a constructor that takes an *owner* and passes it to the base. Let's see what that looks like in practice.
|
@@ -843,6 +849,7 @@ This requires that all members of the class hierarchy agree on the return value
|
843 | 849 | * [winrt::make function template](/uwp/cpp-ref-for-winrt/make)
|
844 | 850 | * [winrt::make_self function template](/uwp/cpp-ref-for-winrt/make-self)
|
845 | 851 | * [winrt::Windows::Foundation::IUnknown::as function](/uwp/cpp-ref-for-winrt/windows-foundation-iunknown#iunknownas-function)
|
| 852 | +* [winrt::Windows::Foundation::IUnknown::try_as function](/uwp/cpp-ref-for-winrt/windows-foundation-iunknown#iunknowntry_as-function) |
846 | 853 |
|
847 | 854 | ## Related topics
|
848 | 855 | * [Author events in C++/WinRT](./author-events.md)
|
|
0 commit comments