'Button_Click' : is not a member of 'ButtonTest::MainPage'

帅比萌擦擦* 提交于 2019-12-12 01:02:59

问题


I'm new to C++ and Windows Store programming, and I'm stuck defining click handlers on buttons. I'm referring to these instructions:

  1. Add a Button control to a parent container.
  2. 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.
  3. To assign a label to the button, set the Content property to a string value.
  4. 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 the Grid block in MainPage.xaml.
  • I've added void MainPage::Button_Click(Object^ sender, RoutedEventArgs^ e) {...} in MainPage.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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!