중요합니다
C++/WinRT를 사용하여 런타임 클래스를 사용하고 작성하는 방법에 대한 이해를 지원하는 필수 개념 및 용어는 C++/WinRT 및 C++/WinRT를 사용하여 API 사용 및 API 작성을 참조하세요.
MeasureOverride 및 OnApplyTemplate과 같은 재정의 가능한 메서드 구현
XAML에는 애플리케이션이 연결할 수 있는 몇 가지 확장 지점이 있습니다. 예를 들면 다음과 같습니다.
기본 런타임 클래스에서 추가로 파생되는 Control 런타임 클래스에서 사용자 지정 컨트롤을 파생합니다.
overridable 또한 파생 클래스에서 재정의할 수 있는 Control, 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 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