更新: 我使用 Richard 的建议来修复 Tag 的设置。但是我在使用 getter for Tag 并在其上使用.as / try_as运算符时遇到了一些问题。
class DerivedController : public winrt::implements<DerivedController, Controller> {
public:
DerivedController() {}
virtual ~DerivedController() {}
static winrt::com_ptr<DerivedController> from(const winrt::FrameworkElement& control) {
return control ? control.Tag().try_as<DerivedController>() : nullptr;
}
}
这是我得到的错误:
1>C:\Program Files (x86)\Windows Kits\10\Include\10.0.18362.0\cppwinrt\winrt/base.h(8013): error C2440: 'static_cast': cannot convert from 'winrt::impl::producer<D,I,void> *' to 'D *'
1> with
1> [
1> D=`anonymous-namespace'::DerivedController,
1> I=winrt::Windows::Foundation::IInspectable
1> ]
1> and
1> [
1> D=`anonymous-namespace'::DerivedController
1> ]
1>C:\Program Files (x86)\Windows Kits\10\Include\10.0.18362.0\cppwinrt\winrt/base.h(8013): note: Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
1>C:\Program Files (x86)\Windows Kits\10\Include\10.0.18362.0\cppwinrt\winrt/base.h(8012): note: while compiling class template member function 'D &winrt::impl::produce_base<D,I,void>::shim(void) noexcept'
1> with
1> [
1> D=`anonymous-namespace'::DerivedController,
1> I=winrt::Windows::Foundation::IInspectable
1> ]
1>C:\Program Files (x86)\Windows Kits\10\Include\10.0.18362.0\cppwinrt\winrt/base.h(7777): note: see reference to function template instantiation 'D &winrt::impl::produce_base<D,I,void>::shim(void) noexcept' being compiled
1> with
1> [
1> D=`anonymous-namespace'::DerivedController,
1> I=winrt::Windows::Foundation::IInspectable
1> ]
1>C:\Program Files (x86)\Windows Kits\10\Include\10.0.18362.0\cppwinrt\winrt/base.h(7737): note: see reference to class template instantiation 'winrt::impl::produce_base<D,I,void>' being compiled
1> with
1> [
1> D=`anonymous-namespace'::DerivedController,
1> I=winrt::Windows::Foundation::IInspectable
1> ]
1>C:\Program Files (x86)\Windows Kits\10\Include\10.0.18362.0\cppwinrt\winrt/base.h(7777): note: see reference to class template instantiation 'winrt::impl::produce<D,winrt::Windows::Foundation::IInspectable>' being compiled
1> with
1> [
1> D=`anonymous-namespace'::DerivedController
1> ]
1>C:\Program Files (x86)\Windows Kits\10\Include\10.0.18362.0\cppwinrt\winrt/base.h(4088): note: see reference to function template instantiation 'D *winrt::get_self<To,winrt::Windows::Foundation::IInspectable>(const I &) noexcept' being compiled
1> with
1> [
1> D=`anonymous-namespace'::DerivedController,
1> To=`anonymous-namespace'::DerivedController,
1> I=winrt::Windows::Foundation::IInspectable
1> ]
1>C:\Program Files (x86)\Windows Kits\10\Include\10.0.18362.0\cppwinrt\winrt/base.h(2387): note: see reference to function template instantiation 'winrt::com_ptr<D> winrt::impl::as<To,winrt::impl::IUnknown>(From *)' being compiled
1> with
1> [
1> D=`anonymous-namespace'::DerivedController,
1> To=`anonymous-namespace'::DerivedController,
1> From=winrt::impl::IUnknown
1> ]
1>note: see reference to function template instantiation 'winrt::com_ptr<D> winrt::Windows::Foundation::IUnknown::as<`anonymous-namespace'::DerivedController>(void) const' being compiled
1> with
1> [
1> D=`anonymous-namespace'::DerivedController
1> ]
1>note: see reference to class template instantiation 'winrt::com_ptr<D>' being compiled
1> with
1> [
1> D=Controller
1> ]
1>C:\Program Files (x86)\Windows Kits\10\Include\10.0.18362.0\cppwinrt\winrt/base.h(10615): note: see reference to class template instantiation 'winrt::com_ptr<winrt::impl::IContextCallback>' being compiled (compiling source file
1>C:\Program Files (x86)\Windows Kits\10\Include\10.0.18362.0\cppwinrt\winrt/base.h(10349): note: see reference to class template instantiation 'winrt::com_ptr<winrt::impl::IServerSecurity>' being compiled
1>C:\Program Files (x86)\Windows Kits\10\Include\10.0.18362.0\cppwinrt\winrt/base.h(10308): note: see reference to class template instantiation 'std::chrono::time_point<winrt::clock,winrt::Windows::Foundation::TimeSpan>' being compiled
1>C:\Program Files (x86)\Windows Kits\10\Include\10.0.18362.0\cppwinrt\winrt/base.h(6462): note: see reference to class template instantiation 'winrt::com_ptr<winrt::impl::IMarshal>' being compiled
1>C:\Program Files (x86)\Windows Kits\10\Include\10.0.18362.0\cppwinrt\winrt/base.h(210): note: see reference to class template instantiation 'std::array<uint8_t,8>' being compiled
控制器是构造函数:
auto controller = winrt::make<Controller>().as<Controller>();
DerivedController 构造为:
DerivedController dController{};
DerivedController 在 c++/cx 中曾经是这样的:
ref class DerivedController sealed : public Controller {
internal : explicit DerivedController(Windows::UI::Xaml::FrameworkElement ^ &control)
: Controller(control) {}
static DerivedController ^
from(Windows::UI::Xaml::FrameworkElement ^ control) {
return control ? dynamic_cast<SvgCanvasController ^>(control->Tag) : nullptr;
}
}
不知道我做错了什么,错误似乎与类的定义方式有关。将不胜感激任何想法!
原文: 在 C++/CX 中,我曾经能够做到:
ref class Controller {
Controller() {
container = ref new UserControl();
container->Tag = this;
...
...
}
}
当我尝试将其转换为 C++/WinRT 时,直接转换如下:
class Controller : public winrt::implements<Controller, winrt::Windows::Foundation::IInspectable> {
Controller::Controller() {
winrt::UserControl container;
1===> container.Tag(this);
...
...
}
}
Controller 是一个手工编写的类(没有 idls),其定义如下所示:
class Controller : public winrt::implements<Controller, winrt::Windows::Foundation::IInspectable>
{
...
...
...
}
但我在 (1) 处收到错误:
Error C2664 'void winrt::impl::consume_Windows_UI_Xaml_IFrameworkElement<D>::Tag(const winrt::Windows::Foundation::IInspectable &) const': cannot convert argument 1 from 'Controller *' to 'const winrt::Windows::Foundation::IInspectable &'
- 是否可以使用与 ABI 的某些互操作将 Com 指针设置为 Tag?
- 还有什么我想念的吗?
- 这是正确的方法吗?或者有没有办法解决它?