如何在重写中调用基类实现TextBox :: OnKeyDown()? [英] How do I call the base class implementation TextBox::OnKeyDown() in my override?

查看:128
本文介绍了如何在重写中调用基类实现TextBox :: OnKeyDown()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了 TextBox 类的子类,以便可以在 OnKeyDown()方法中捕获所有键盘事件( KeyDown事件不会触发用于所有键盘事件,包括但不限于退格键和箭头键,但OnKeyDown会).

I have created a subclass of the TextBox class so that I can capture all keyboard events in the OnKeyDown() method (the KeyDown event will not trigger for all keyboard events, including but not limited to, backspace and arrow-keys, but OnKeyDown will).

此问题是由于它完全绕开了控件的内部键盘处理,因此它有效地禁用了TextBox.显而易见的解决方案是在超类中调用OnKeyDown.

The problem with this is that it effectively disables the TextBox, as it bypasses the control's internal keyboard handling entirely. The obvious solution is to call OnKeyDown in the superclass.

如何在Windows Runtime中做到这一点? 仅调用TextBox :: OnKeyDown(...)无效,因为它将有效地带我进行覆盖.

How do I do that in Windows Runtime? Just calling TextBox::OnKeyDown(...) won't work, as it will effectively take me to my override.

OnKeyDown()方法是IControlOverrides接口的一部分,看来我无法为TextBox对象实例获取指向该接口的接口指针,而只能为我的派生对象实例获取.

The OnKeyDown() method is part of the IControlOverrides interface, and it seems I can't get an interface pointer to that interface for the TextBox object instance, but only for my derived object instance.

推荐答案

C ++/CX中的继承与C ++中的继承相同.这是因为C ++/CX引用类实际上是COM对象,它通过各种聚合实现继承.

Inheritance in C++/CX is not the same as inheritance in C++. This is because C++/CX ref classes are in fact COM objects, which implements inheritance through a variety of aggregation.

我正在处理的示例是

public ref class MyTextBox : public TextBox {
  MyTextBox() {};
  ~MyTextBox() {};
  virtual void OnKeyDown(KeyEventArgs^ e) override {
    TextBox::OnKeyDown(e);
  };
};

无效,因为TextBox :: OnKeyDown()将有效地调用MyTextBox :: OnKeyDown().这是因为在C ++/CX和COM中如何实现虚拟方法,因此可以阅读

which won't work, because TextBox::OnKeyDown() will effectively call MyTextBox::OnKeyDown(). This is because of how virtual methods are implemented in C++/CX and COM, an in-depth overview can be read here.

Visual Studio 11开发人员预览解决方案

简短的版本是OnKeyDown()方法是IControlOverrides接口的一部分,该接口由MyTextBox和TextBox共同实现,这要归功于一些聪明的编译器技巧.为了掌握TextBox实现的接口指针,我们首先需要向TextBox询问-如果我们询问MyTextBox,我们将在哪里开始.并且由于这是COM而不是C ++中的继承,因此我们通过指向 baseclass 对象的指针而不是 this :

The short version is that the OnKeyDown() method is part of the IControlOverrides interface, implemented by both MyTextBox and TextBox, thanks to some clever compiler tricks. To get hold of the interface pointer for the TextBox implementation, we first need to ask the TextBox for it - if we ask MyTextBox we end up where we started. And because this is inheritance in COM, not C++, we do that through a pointer to the baseclass object instead of this:

  virtual void OnKeyDown(KeyEventArgs^ e) override {
    struct IControlOverrides^ ico;
    HRESULT hr = __cli_baseclass->__cli_QueryInterface(const_cast<class Platform::Guid%>(reinterpret_cast<const class Platform::Guid%>(__uuidof(struct IControlOverrides^))),reinterpret_cast<void**>(&ico));
    if (!hr) {
        hr = ico->__cli_OnKeyDown(e);
    };
  };

Visual Studio 11 Beta解决方案

与许多其他事情一样,VS 11 Beta中对此进行了改进.虽然"__super ::"仍然无法使用,但现在它可以正常工作,只是将调用显式地调用到定义它的接口即可:

As with so many other things, this has been improved in the VS 11 Beta. While "__super::" still won't work, it now works fine just to make the call explicitly to the interface in which it is defined:

  virtual void OnKeyDown(KeyEventArgs^ e) override {
    IControlOverrides::OnKeyDown(e);
  };

VS中的对象浏览器将向您显示在(IControlOverrides)中定义了OnKeyDown()方法的接口.

The Object Browser in VS will show you which interface the OnKeyDown() method is defined in (IControlOverrides).

问题解决了!

这篇关于如何在重写中调用基类实现TextBox :: OnKeyDown()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆