为什么在子类 Button 控件中重写 OnApplyTemplate() 会出现访问冲突错误?

发布于 2025-01-05 05:15:15 字数 1030 浏览 1 评论 0 原文

我在处理 C++/CX 中的 Windows.UI.Xaml.Button 类子类化时遇到了这个问题,我想知道发生了什么。

如果我对 Button 控件进行子类化,并将子类的实例添加到网格中,则一切都会按预期工作。

如果将 OnApplyTemplate() 的重写方法添加到子类,则在将子类实例添加到网格时会出现 NULL 指针异常。

我的代码大致如下(LayoutRoot 是 MainPage.xaml 中的网格,此示例已在空的简单 Metro 应用程序中进行了测试):

// Scenario 1: This works
LayoutRoot->Children->Append(ref new MyButton1());

// Scenario 2: This doesn't work, it will cause a NULL-pointer exception
LayoutRoot->Children->Append(ref new MyButton2());

// This is how MyButton1 and MyButton2 is defined
public ref class MyButton1 : public Button { 
  public:
    MyButton1() {};
    ~MyButton1() {};
};

public ref class MyButton2 : public Button { 
  public:
    MyButton2() {};
    ~MyButton2() {};
    virtual void OnApplyTemplate() override {};
};

请注意,此问题与 这个问题,但是错误而且场景完全不同,我可以单独发布这个。

I've come across this problem dealing with subclassing the Windows.UI.Xaml.Button class in C++/CX, and I'd like to know what's going on.

If I subclass the Button control, and add an instance of the subclass to a grid, everything works as expected.

If add an override method of OnApplyTemplate() to the subclass, I get NULL-pointerexception when adding the subclass instance to the grid.

My code looks roughly like this (LayoutRoot is a Grid in MainPage.xaml, this sample has been tested in an empty simple metro application):

// Scenario 1: This works
LayoutRoot->Children->Append(ref new MyButton1());

// Scenario 2: This doesn't work, it will cause a NULL-pointer exception
LayoutRoot->Children->Append(ref new MyButton2());

// This is how MyButton1 and MyButton2 is defined
public ref class MyButton1 : public Button { 
  public:
    MyButton1() {};
    ~MyButton1() {};
};

public ref class MyButton2 : public Button { 
  public:
    MyButton2() {};
    ~MyButton2() {};
    virtual void OnApplyTemplate() override {};
};

Note that this question is slightly similar to this question, but the error and the scenario is sufficiently different for me to post this one separately.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

濫情▎り 2025-01-12 05:15:15

更新:

在 Consumer Preview/Visual Studio 11 Beta 中,这个问题已经消失。 OnApplyTemplate() 方法是虚拟的,OnApplyTemplateCore() 方法消失了。我花了一段时间才弄清楚,因为我实现了现已消失的虚拟方法,因此出现了非常奇怪的编译器错误。


旧答案,适用于开发者预览版

答案让我为没有正确阅读文档而感到羞愧:

OnApplyTemplate()方法是< em>不是虚拟的,所以我们不能覆盖它。
通过使用可重写的 OnUpdateTemplateCore() 方法来解决该问题。

出于某种原因,它在某种程度上对我有用(编译器当然对此没有什么可说的),但是覆盖它绝对不是正确的做法

UPDATE:

With the Consumer Preview/Visual Studio 11 Beta, this problem has gone away. The OnApplyTemplate() method is virtual, and the OnApplyTemplateCore() method is gone. It took me a while to figure it out, as I got pretty weird compiler errors because of my implementation of the now gone virtual method.


Old answer, applies to the Developer Preview:

The answer makes me feel ashamed for not reading the documentation properly:

The OnApplyTemplate() method is not virtual, so we can't override it.
The problem is solved by using the overridable OnUpdateTemplateCore() method instead.

For some reason it worked for me up until a certain point anyway (and the compiler certainly didn't have anything to say about it), but overriding it is absolutely not the right thing to do.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文