Page 1 of 1

Button click delegate for class method

Posted: 08 Apr 2015, 02:42
by Rick
I'm looking to make a C++ class function to handle a button click event (and eventually other things). Is there somewhere to see the event signatures to handle this and how to set this up? I'm not sure how to setup the C++ callback member method and how to assign it. I'm thinking it has something to do with the below but I'm not finding any examples that do this.
Button3 = uiRenderer->GetXAML()->FindName<Button>("Button3");
Button3->Click() += MakeDelegate()
Also I'm trying to add this to the class I'm doing the above code in. Some possibly some sort of:

Button3->Click() += MakeDelegate(this, App::Button3_OnClick);

But this doesn't work.

Re: Button click delegate for class method

Posted: 08 Apr 2015, 03:37
by Rick
OK had to dig but found some example code and got this sort of working.
Gui::Button* Button3;
void Button3_OnClick(BaseComponent* sender, const RoutedEventArgs& e);

Button3 = uiRenderer->GetXAML()->FindName<Button>("Button3");
Button3->Click() += MakeDelegate(this, &App::Button3_OnClick);

void App::Button3_OnClick(BaseComponent* sender, const RoutedEventArgs& e)
{
}
The strange thing is this event is firing when I just put the mouse over the control. I'm not clicking at all.

[EDIT]

Ah, it's a hover button I was over! All good.

Re: Button click delegate for class method

Posted: 08 Apr 2015, 16:36
by sfernandez
You can find the signature of each event by looking at the typedef of the event handler.

For example, the Button.Click event is defined as:
UIElement::Event<RoutedEventHandler> Click(); 
And if you go to the RoutedEventHandler definition, it looks like:
typedef Delegate<void (BaseComponent*, const RoutedEventArgs&)> RoutedEventHandler;
So the signature of your Click event handler should be:
void OnButtonClick(BaseComponent* sender, const RoutedEventArgs& e);

Re: Button click delegate for class method

Posted: 08 Apr 2015, 20:24
by Rick
Thank you for the explanation. I'm person who learns better with english explanations vs weeding through code. This helped a lot.