如何在代码中创建XAML自定义控件?(How to create a XAML custom control in code?)

编程入门 行业动态 更新时间:2024-10-14 04:25:12
如何在代码中创建XAML自定义控件?(How to create a XAML custom control in code?)

我正在尝试使用C ++ / WinRT在代码中实现自定义XAML控件。 但是,我尝试的实现无法编译。 作为概念的证明,我使用了这段代码:

#pragma once

#include <winrt/Windows.UI.Xaml.Controls.h>

namespace MyApp
{
    struct MyControl : winrt::implements<MyControl, winrt::Windows::UI::Xaml::Controls::Control>
    {
    };
}
 

这导致以下编译器错误:

1>MyControl.cpp
1>c:\program files (x86)\windows kits\10\include\10.0.17134.0\cppwinrt\winrt\base.h(6416): error C2079: 'winrt::impl::producer<D,winrt::Windows::UI::Xaml::Controls::Control,void>::vtable' uses undefined struct 'winrt::impl::produce<D,I>'
1>        with
1>        [
1>            D=MyApp::MyControl
1>        ]
1>c:\program files (x86)\windows kits\10\include\10.0.17134.0\cppwinrt\winrt\base.h(7163): note: see reference to class template instantiation 'winrt::impl::producer<D,winrt::Windows::UI::Xaml::Controls::Control,void>' being compiled
1>        with
1>        [
1>            D=MyApp::MyControl
1>        ]
1>c:\xxx\mycontrol.h(8): note: see reference to class template instantiation 'winrt::implements<MyApp::MyControl,winrt::Windows::UI::Xaml::Controls::Control>' being compiled
 

我无法理解编译器错误。 显然,您无法像实现Windows运行时使用的其他类型一样实现XAML控件。

在代码中实现XAML自定义控件需要什么?

I am trying to implement a custom XAML control in code, using C++/WinRT. My attempted implementation, however, failed to compile. As a prove of concept I was using this code:

#pragma once

#include <winrt/Windows.UI.Xaml.Controls.h>

namespace MyApp
{
    struct MyControl : winrt::implements<MyControl, winrt::Windows::UI::Xaml::Controls::Control>
    {
    };
}
 

This resulted in the following compiler error:

1>MyControl.cpp
1>c:\program files (x86)\windows kits\10\include\10.0.17134.0\cppwinrt\winrt\base.h(6416): error C2079: 'winrt::impl::producer<D,winrt::Windows::UI::Xaml::Controls::Control,void>::vtable' uses undefined struct 'winrt::impl::produce<D,I>'
1>        with
1>        [
1>            D=MyApp::MyControl
1>        ]
1>c:\program files (x86)\windows kits\10\include\10.0.17134.0\cppwinrt\winrt\base.h(7163): note: see reference to class template instantiation 'winrt::impl::producer<D,winrt::Windows::UI::Xaml::Controls::Control,void>' being compiled
1>        with
1>        [
1>            D=MyApp::MyControl
1>        ]
1>c:\xxx\mycontrol.h(8): note: see reference to class template instantiation 'winrt::implements<MyApp::MyControl,winrt::Windows::UI::Xaml::Controls::Control>' being compiled
 

I am unable to understand the compiler error. Apparently, you cannot implement a XAML control the same way you would implement other types for use by the Windows Runtime.

What is required to implement a XAML custom control in code?

最满意答案

WinRT中的“继承”或“子类化”与C ++继承略有不同。 因为这些是COM接口,所以当您继承WinRT运行时类时,您实际所做的是COM聚合 ,并结合实现基类型的可覆盖接口 。 由于COM聚合方面,这比标准C ++继承更加繁琐,所有委托/非委托,特殊构造等等。这将是WRL的一个主要痛苦,但C ++ / CX在把它抽象出去的引擎盖。 幸运的是,C ++ / WinRT可以帮助您提供两种类型的抽象,而不需要使用隐形魔法。

如果您正在创作一个不需要外部可见的类型(例如,应用程序,而不是运行时组件),C ++ / WinRT为此提供了方便的帮助:

#pragma once #include <winrt/Windows.UI.Xaml.Controls.h> namespace MyApp { struct MyControl : winrt::Windows::UI::Xaml::Controls::ControlT<MyControl> { void OnTapped(winrt::Windows::UI::Xaml::Input::TappedRoutedEventArgs const&); }; }

此基类型ControlT将正确构造聚合的基本Control实例并将基本方法委托给它,同时还实现“可覆盖”接口。 这些可覆盖的方法都给出了一个占位符实现,默认调用基本方法,但您可以自己覆盖它们并获得自定义行为。

另一方面,如果您需要通过IDL创建一个预计的类型:

namespace MyApp { [default_interface] runtimeclass MyControl : Windows.UI.Xaml.Controls.Control { MyControl(); }; }

这将产生与上面的内置ControlT案例类似的脚手架,但也会预测您的类型。 实际上,如果你检查这个类型的生成文件(在这个例子中,MyControl.gh),你会看到一个MyControlT ,它将全部连接起来。

(注意:只有当你有一个空的,可构造的,密封的运行时类时,才需要[default_interface]属性。一旦你添加成员,midl将会合成默认接口,而不需要任何其他的哄骗。

"Inheriting" or "subclassing" in WinRT is subtly different from C++ inheritance. Because these are COM interfaces, when you subclass a WinRT runtimeclass, what you're really doing is COM Aggregation, combined with implementing the base type's overridable interfaces. Due to the COM aggregation aspect, this is considerably more fussy than standard C++ inheritance, what with all the delegating/nondelegating, special construction, etc. This would be a major pain in WRL, but C++/CX did a bunch of compiler magic under the hood to abstract this away. Fortunately, C++/WinRT helps you out here with providing two types of abstractions, without resorting to invisible magic.

If you are authoring a type that doesn't need to be externally visible (e.g. an app, as opposed to a runtime component) C++/WinRT provides convenient helpers for this:

#pragma once #include <winrt/Windows.UI.Xaml.Controls.h> namespace MyApp { struct MyControl : winrt::Windows::UI::Xaml::Controls::ControlT<MyControl> { void OnTapped(winrt::Windows::UI::Xaml::Input::TappedRoutedEventArgs const&); }; }

This base type ControlT will correctly construct the aggregated base Control instance and delegate base methods to it, while also implementing the "overridable" interfaces. These overridable methods are all given a placeholder implementation that defaults to calling the base method, but you can override them yourself and get your custom behavior.

If, on the other hand, you need to author a type that's projected, via IDL:

namespace MyApp { [default_interface] runtimeclass MyControl : Windows.UI.Xaml.Controls.Control { MyControl(); }; }

That will generate similar scaffolding as the built-in ControlT case above, but also projects your type. In fact, if you examine the generated file for this type (in this example, MyControl.g.h), you'd see a MyControlT where that gets all hooked up.

(Note: the [default_interface] attribute is only needed if you have an empty, constructible, sealed runtimeclass. Once you add members, midl will figure synthesize the default interface without any other coaxing.

更多推荐

Windows,Controls,XAML,Xaml,winrt,电脑培训,计算机培训,IT培训"/> <meta name=&q

本文发布于:2023-08-07 00:49:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1459421.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:自定义   控件   代码   如何在   control

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!