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?
implements<T>
class template is pretty low level in the sense that it only helps you implement interfaces.Control
is not an interface, which is why it doesn't work. You would have to list all of the interfaces that you need to implement. The trouble is that controls are pretty complicated, which is why the authoring support provided through the project templates is helpful. If nothing else, you should try the-component
option to get the scaffolding generated for you. – Rustlenamespace MyApp { runtimeclass MyControl : Windows.UI.Xaml.Controls.Control {} }
. The tooling did generate implementation files, too, but those had unexpected contents (essentiallynamespace winrt::MyApp::implementation { struct MyControl { MyControl() = delete; }; }
). If deriving from a concrete implementation is not supported, I would have expected an error from the build tools. There's alsostruct ControlT
in Windows.UI.Xaml.Controls.h, but I'm not sure, what that is all about. – Pyrostat