2

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...

aescript
  • 1,775
  • 4
  • 20
  • 30
  • https://stackoverflow.com/questions/6644247/simple-custom-event – Jason Oct 22 '20 at 20:49
  • do you know [how events work?](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/events/) – Gusman Oct 22 '20 at 20:54
  • yes I understand events.... but i cant link to it through XAML. This requires a Command accorindg to everyone else, which I cant get working. – aescript Oct 22 '20 at 20:58
  • @Jason that looks pretty inflexible and im not sure that will work in my case. This control is going on each page of a carousel, and then a few other places, sometimes bound to different items and i need to update certain restful APIs depending which is run. Also none of it shows it being possible to bind to in XAML – aescript Oct 22 '20 at 21:00
  • You should be able to assign an event handler to a custom event in XAML – Jason Oct 22 '20 at 21:04
  • No matter what i do... event handlers, commands, etc - every time i try to assign it i get compile errors about it being the wrong type No property, BindableProperty, or event found for "OnCountUpdated", or mismatching type between value and property. Im getting seriosuly frustrated... this is not that hard of a concept – aescript Oct 22 '20 at 21:06
  • since you haven't shown us what you are trying or what specific errors you are getting it's impossible to say what you might be doing wrong. Here is the actual source of the Entry TextChanged - https://github.com/xamarin/Xamarin.Forms/blob/f35ae07a0a8471d255f7a1ebdd51499e10e0a4cb/Xamarin.Forms.Core/InputView.cs#L94 – Jason Oct 22 '20 at 21:08
  • i explained it because im not really sure how to show you haha... i havent even come CLOSE to something that works. – aescript Oct 22 '20 at 21:11
  • Although, that last link may be my answer... – aescript Oct 22 '20 at 21:12
  • It looks like the issue was because i am a fool. Everytime i added my event to the custom control class i would do || public EventHandler CountUpdated; || intead of || public event Eventhandler... – aescript Oct 22 '20 at 21:16
  • Alternatively, create a command property for binding, and execute the command in your event handler. – Shaw Oct 22 '20 at 23:09
  • @aescript Hi, if you have understanded and solved it , you could update it in answer when you have time. – Junior Jiang Oct 23 '20 at 02:06

1 Answers1

2

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.

aescript
  • 1,775
  • 4
  • 20
  • 30