Update:
Based on Ryan Shepherd's reply, I changed my code to:
class DerivedClass : winrt::implements<DerivedClass, BaseClass> {
public:
DerivedClass(winrt::FrameworkElement& control) :
5===>implements_type(control) {}
static winrt::com_ptr<DerivedClass> from(winrt::FrameworkElement control) {
control ? control.Tag().try_as<SvgCanvasController>() : nullptr;
}
}
Now I get an error:
no instance of constructor "winrt::implements<D, I, ...>::implements [with D=DerivedClass, I=BaseClass]" matches the argument list argument types are: (winrt::Windows::UI::Xaml::FrameworkElement).
I do have BaseClass Constructor defined which takes in &FrameworkElement as an argument. So I am not sure what I am getting this error.
Original:
I had this in C++/CX:
ref class DerivedClass sealed : public BaseClass {
public:
explicit DerivedClass(Windows::UI::Xaml::FrameworkElement^ &control)
: BaseClass(control) {}
static DerivedClass from(Windows::UI::Xaml::FrameworkElement^ control) {
return control ? dynamic_cast<DerivedClass>(control->Tag) : nullptr;
}
}
when I try to convert this to c++/winrt
class DerivedClass : winrt::implements<DerivedClass, BaseClass> {
public:
1===>DerivedClass(winrt::FrameworkElement& control) : BaseClass(control)
2===>{
}
static DerivedClass from(winrt::FrameworkElement control) {
if (control)
3===> return control.Tag().try_as<SvgCanvasController>();
else
4===> return nullptr;
}
There are a few issues here I want to discuss and understand. As it is written above, I get the following errors:
1
BaseClass is not a non-static data member or base class of class DerivedClass
2
default constructor of winrt::implements<DerivedClass, BaseClass> cannot be referenced -- it is a deleted function
3
No suitable user defined conversion from winrt::impl::com_ref<DerivedClass> to DerivedClass
4
no suitable constructor exists to convert from std::nullptr_t to DerivedClass
Would love some thoughts on these errors.
I understand I create a DerivedClass object as
auto obj = winrt::make<DerivedClass>();
Questions:
- How do I write the constructor properly?
- How can i pass the argument to the base class constructor like I do in C++/CX?
- How can I deal with the errors in the from() function definition.