Skip to content

Commit ffaaadf

Browse files
authored
refactoring content into a new topic (#3763)
1 parent ed63e33 commit ffaaadf

File tree

3 files changed

+95
-57
lines changed

3 files changed

+95
-57
lines changed

uwp/cpp-and-winrt-apis/base-type.md

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
---
2+
title: Calling and overriding your base type with C++/WinRT
3+
description: How to call and override your base type with C++/WinRT.
4+
ms.date: 09/11/2023
5+
ms.topic: article
6+
keywords: windows 10, uwp, standard, c++, cpp, winrt, projection, XAML, access, call, base type, base, type
7+
ms.localizationpriority: medium
8+
---
9+
10+
# Calling and overriding your base type with C++/WinRT
11+
12+
> [!IMPORTANT]
13+
> For essential concepts and terms that support your understanding of how to consume and author runtime classes with [C++/WinRT](./intro-to-using-cpp-with-winrt.md), see [Consume APIs with C++/WinRT](consume-apis.md) and [Author APIs with C++/WinRT](author-apis.md).
14+
15+
## Implementing *overridable* methods, such as **MeasureOverride** and **OnApplyTemplate**
16+
17+
There are some extension points in XAML that your application can plug into, for example:
18+
19+
- [MeasureOverride](/uwp/api/windows.ui.xaml.frameworkelement.measureoverride)
20+
- [OnApplyTemplate](/uwp/api/windows.ui.xaml.frameworkelement.onapplytemplate)
21+
- [GoToStateCore](/uwp/api/windows.ui.xaml.visualstatemanager.gotostatecore)
22+
23+
You derive a custom control from the [**Control**](/uwp/api/windows.ui.xaml.controls.control) runtime class, which itself further derives from base runtime classes. And there are `overridable` methods of **Control**, [**FrameworkElement**](/uwp/api/windows.ui.xaml.frameworkelement), and [**UIElement**](/uwp/api/windows.ui.xaml.uielement) that you can override in your derived class. Here's a code example showing you how to do that.
24+
25+
```cppwinrt
26+
struct BgLabelControl : BgLabelControlT<BgLabelControl>
27+
{
28+
...
29+
// Control overrides.
30+
void OnPointerPressed(Windows::UI::Xaml::Input::PointerRoutedEventArgs const& /* e */) const { ... };
31+
32+
// FrameworkElement overrides.
33+
Windows::Foundation::Size MeasureOverride(Windows::Foundation::Size const& /* availableSize */) const { ... };
34+
void OnApplyTemplate() const { ... };
35+
36+
// UIElement overrides.
37+
Windows::UI::Xaml::Automation::Peers::AutomationPeer OnCreateAutomationPeer() const { ... };
38+
...
39+
};
40+
```
41+
42+
*Overridable* methods present themselves differently in different language projections. In C#, for example, overridable methods typically appear as protected virtual methods. In C++/WinRT, they're neither virtual nor protected, but you can still override them and provide your own implementation, as shown above.
43+
44+
If you're overriding one of these overridable methods in C++/WinRT, then your `runtimeclass` IDL mustn't declare the method.
45+
46+
**IDL**
47+
48+
```ìdl
49+
namespace Example
50+
{
51+
runtimeclass CustomVSM : Windows.UI.Xaml.VisualStateManager
52+
{
53+
CustomVSM();
54+
// note that we don't declare GoToStateCore here
55+
}
56+
}
57+
```
58+
59+
**C++/WinRT**
60+
61+
```cppwinrt
62+
namespace winrt::Example::implementation
63+
{
64+
struct CustomVSM : CustomVSMT<CustomVSM>
65+
{
66+
CustomVSM() {}
67+
68+
bool GoToStateCore(winrt::Windows::UI::Xaml::Controls::Control const& control, winrt::Windows::UI::Xaml::FrameworkElement const& templateRoot, winrt::hstring const& stateName, winrt::Windows::UI::Xaml::VisualStateGroup const& group, winrt::Windows::UI::Xaml::VisualState const& state, bool useTransitions) {
69+
return base_type::GoToStateCore(control, templateRoot, stateName, group, state, useTransitions);
70+
}
71+
};
72+
}
73+
```
74+
75+
## Calling your base type
76+
77+
You can access your bass type, and call methods on it, by using the type alias `base_type`. This is just an example of the syntax of calling `base_type` (this isn't a full illustration of how to perform layout in a custom layout panel):
78+
79+
```cppwinrt
80+
struct CustomGrid : CustomGridT<CustomGrid>
81+
{
82+
CustomGrid() {}
83+
84+
Size ArrangeOverride(Size const& finalSize)
85+
{
86+
return base_type::ArrangeOverride(finalSize);
87+
}
88+
};
89+
```
90+
91+
## Important APIs
92+
* [Control class](/uwp/api/windows.ui.xaml.controls.control)

uwp/cpp-and-winrt-apis/xaml-cust-ctrl.md

Lines changed: 1 addition & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -184,63 +184,7 @@ This walkthrough showed a simple example of a custom (templated) control in C++/
184184

185185
## Implementing *overridable* methods, such as **MeasureOverride** and **OnApplyTemplate**
186186

187-
There are some extension points in XAML that your application can plug into, for example:
188-
189-
- [MeasureOverride](/uwp/api/windows.ui.xaml.frameworkelement.measureoverride)
190-
- [OnApplyTemplate](/uwp/api/windows.ui.xaml.frameworkelement.onapplytemplate)
191-
- [GoToStateCore](/uwp/api/windows.ui.xaml.visualstatemanager.gotostatecore)
192-
193-
You derive a custom control from the [**Control**](/uwp/api/windows.ui.xaml.controls.control) runtime class, which itself further derives from base runtime classes. And there are `overridable` methods of **Control**, [**FrameworkElement**](/uwp/api/windows.ui.xaml.frameworkelement), and [**UIElement**](/uwp/api/windows.ui.xaml.uielement) that you can override in your derived class. Here's a code example showing you how to do that.
194-
195-
```cppwinrt
196-
struct BgLabelControl : BgLabelControlT<BgLabelControl>
197-
{
198-
...
199-
// Control overrides.
200-
void OnPointerPressed(Windows::UI::Xaml::Input::PointerRoutedEventArgs const& /* e */) const { ... };
201-
202-
// FrameworkElement overrides.
203-
Windows::Foundation::Size MeasureOverride(Windows::Foundation::Size const& /* availableSize */) const { ... };
204-
void OnApplyTemplate() const { ... };
205-
206-
// UIElement overrides.
207-
Windows::UI::Xaml::Automation::Peers::AutomationPeer OnCreateAutomationPeer() const { ... };
208-
...
209-
};
210-
```
211-
212-
*Overridable* methods present themselves differently in different language projections. In C#, for example, overridable methods typically appear as protected virtual methods. In C++/WinRT, they're neither virtual nor protected, but you can still override them and provide your own implementation, as shown above.
213-
214-
If you're overriding one of these overridable methods in C++/WinRT, then your `runtimeclass` IDL mustn't declare the method.
215-
216-
**IDL**
217-
218-
```ìdl
219-
namespace Example
220-
{
221-
runtimeclass CustomVSM : Windows.UI.Xaml.VisualStateManager
222-
{
223-
CustomVSM();
224-
// note that we don't declare GoToStateCore here
225-
}
226-
}
227-
```
228-
229-
**C++/WinRT**
230-
231-
```cppwinrt
232-
namespace winrt::Example::implementation
233-
{
234-
struct CustomVSM : CustomVSMT<CustomVSM>
235-
{
236-
CustomVSM() {}
237-
238-
bool GoToStateCore(winrt::Windows::UI::Xaml::Controls::Control const& control, winrt::Windows::UI::Xaml::FrameworkElement const& templateRoot, winrt::hstring const& stateName, winrt::Windows::UI::Xaml::VisualStateGroup const& group, winrt::Windows::UI::Xaml::VisualState const& state, bool useTransitions) {
239-
return base_type::GoToStateCore(control, templateRoot, stateName, group, state, useTransitions);
240-
}
241-
};
242-
}
243-
```
187+
See the section in [Calling and overriding your base type with C++/WinRT](base-type.md#implementing-overridable-methods-such-as-measureoverride-and-onapplytemplate).
244188

245189
## Important APIs
246190
* [Control class](/uwp/api/windows.ui.xaml.controls.control)

uwp/develop/toc.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -772,6 +772,8 @@
772772
href: ../cpp-and-winrt-apis/binding-collection.md
773773
- name: XAML custom (templated) controls
774774
href: ../cpp-and-winrt-apis/xaml-cust-ctrl.md
775+
- name: Calling and overriding your base type
776+
href: ../cpp-and-winrt-apis/base-type.md
775777
- name: Passing parameters into the ABI boundary
776778
href: ../cpp-and-winrt-apis/pass-parms-to-abi.md
777779
- name: Consume COM components

0 commit comments

Comments
 (0)