Important
C++/WinRT を使用してランタイム クラスを使用および作成する方法の理解をサポートする基本的な概念と用語については、「 C++/WinRTでの API の使用」および「C++/WinRT を使用した API の作成」を参照してください。
MeasureOverride や OnApplyTemplate などのオーバーライド可能なメソッドの実装
XAML には、アプリケーションがプラグインできる拡張ポイントがいくつかあります。次に例を示します。
コントロール ランタイム クラス からカスタム コントロールを派生させます。このクラス自体は、基底ランタイム クラスからさらに派生します。 また、派生クラスでオーバーライドできる overridable、FrameworkElement、UIElement のメソッドがあります。 その方法を示すコード例を次に示します。
struct BgLabelControl : BgLabelControlT<BgLabelControl>
{
...
// Control overrides.
void OnPointerPressed(Microsoft::UI::Xaml::Input::PointerRoutedEventArgs const& /* e */) const { ... };
// FrameworkElement overrides.
Windows::Foundation::Size MeasureOverride(Windows::Foundation::Size const& /* availableSize */) const { ... };
void OnApplyTemplate() const { ... };
// UIElement overrides.
Microsoft::UI::Xaml::Automation::Peers::AutomationPeer OnCreateAutomationPeer() const { ... };
...
};
オーバーライド可能な メソッドは、言語プロジェクションごとに異なる方法で表示されます。 たとえば、C# では、オーバーライド可能なメソッドは通常、保護された仮想メソッドとして表示されます。 C++/WinRT では、これらは仮想でも保護もされませんが、上記のようにオーバーライドして独自の実装を提供することはできます。
C++/WinRT でこれらのオーバーライド可能なメソッドの 1 つをオーバーライドする場合、 runtimeclass IDL でメソッドを宣言することはできません。 示されている base_type 構文の詳細については、このトピックの次のセクションを参照してください (基本型の呼び出し)。
Idl
namespace Example
{
runtimeclass CustomVSM : Microsoft.UI.Xaml.VisualStateManager
{
CustomVSM();
// note that we don't declare GoToStateCore here
}
}
C++/WinRT
namespace winrt::Example::implementation
{
struct CustomVSM : CustomVSMT<CustomVSM>
{
CustomVSM() {}
bool GoToStateCore(winrt::Microsoft::UI::Xaml::Controls::Control const& control, winrt::Microsoft::UI::Xaml::FrameworkElement const& templateRoot, winrt::hstring const& stateName, winrt::Microsoft::UI::Xaml::VisualStateGroup const& group, winrt::Microsoft::UI::Xaml::VisualState const& state, bool useTransitions) {
return base_type::GoToStateCore(control, templateRoot, stateName, group, state, useTransitions);
}
};
}
基底型を呼び出す
基本型にアクセスし、その基本型のメソッドを呼び出すには、型エイリアス base_typeを使用します。 前のセクションでこの例を見ました。ただし、 base_type を使用して、(オーバーライドされたメソッドだけでなく) 任意の基底クラス メンバーにアクセスできます。 次に例を示します。
struct MyDerivedRuntimeClass : MyDerivedRuntimeClassT<MyDerivedRuntimeClass>
{
...
void Foo()
{
// Call my base type's Bar method.
base_type::Bar();
}
};
重要な API
Windows developer