问题
I'm new to C++ and Windows Store programming, and I'm stuck defining click handlers on buttons. I'm referring to these instructions:
- Add a Button control to a parent container.
- To assign a name to the button, set the x:Name attribute to a string value. To refer to a control in code, it must have a name. Otherwise, a name is not required.
- To assign a label to the button, set the Content property to a string value.
- To perform an action when a user clicks the button, add a handler for the Click event. In the Click event handler, add code to perform some action.
<Button x:Name="button1" Content="Button" Click="Button_Click" />
void MainPage::Button_Click(Object^ sender, RoutedEventArgs^ e) {
// Add code to perform some action here.
}
- I've added the
<Button x:Name="button1" Content="Button" Click="Button_Click" />
inside theGrid
block inMainPage.xaml
. - I've added
void MainPage::Button_Click(Object^ sender, RoutedEventArgs^ e) {...}
inMainPage.xaml.cpp
.
Now I get two errors, which I can't resolve:
error C2039: 'Button_Click' : is not a member of 'ButtonTest::MainPage'
and
IntelliSense: class "ButtonTest::MainPage" has no member "Button_Click"
How can I solve this?
回答1:
You will need to define the prototype of MainPage::Button_Click
in the MainPage.xaml.h
file as a member of the class. Like
public:
Button_Click(object^, RoutedEventArgs^);
C++ needs a prototype for every method.
来源:https://stackoverflow.com/questions/15888991/button-click-is-not-a-member-of-buttontestmainpage