c# xamarin forms - custom event on custom control with a property

Viewed 660

Okay, im positive this has an answer somewhere but I have been banging my head against a wall FOREVER trying to get this to work, an working around it for days, and im losing my mind here. I cannot find a single example that works or does what I want... at least not that I understand how its written.

Im writing a custom control, basically a content view with a calculator in it. One of the controls in this is an entry.

What i want is VERY simple... when you create an entry in XAML you can do

<Entry TextChanged="FunctionToRun">

and then whenever the text is changed, an event is fired and that function is run.

In my case i want to add a custom event to my calculator class so that when i create one on a page:

<local:myCalculator CountUpdated="FunctionToRun">

that function gets run.

Everything I look at online talks about using an ICommand and all this - but literally every single example I have tried leads me to either:

A) Not be able to link my function in XAML (errors) B) Only calls something inside the calculator class.... but doesnt trigger any events, and i cannot force it to.

I think i completely do not understand ICommand, and no matter how many examples I ahve looked at I cannot get what im after.

Anyone able to help? im sure its stupidly simple...

1 Answers

Turns out I was being completely blind and didnt realize i had implemented my event on the item with:

public EventHandler<EventArgs> EventName = {get;set;}

Which is absolutely not how you do it - I wont rewrite the reasoning when I can just find an existing answer: event EventHandler vs EventHandler

Why do we need the "event" keyword while defining events?

Anyhow - it was a blank-minded mistake while coding a lot at once. This should be

public event EventHandler<EventArgs> EventName;

for many reasons - one of the most minor being it allows you to bind properly from XAML.

Related