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.