|
| 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) |
0 commit comments