简体   繁体   English

c#xamarin forms-自定义控件上的自定义事件具有属性

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

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我想要的是非常简单......当你在 XAML 中创建一个条目时,你可以做

<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:我在网上看到的所有内容都在谈论使用 ICommand 以及所有这些 - 但实际上我尝试过的每个示例都引导我:

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. A) 无法在 XAML 中链接我的函数(错误) B) 仅调用计算器类中的某些内容....但不会触发任何事件,我无法强制它。

I think i completely do not understand ICommand, and no matter how many examples I ahve looked at I cannot get what im after.我想我完全不理解 ICommand,无论我看过多少例子,我都无法得到我想要的。

Anyone able to help?任何人都可以提供帮助? im sure its stupidly simple...我确定它非常简单......

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这绝对不是你怎么做的 - 当我能找到一个现有的答案时,我不会重写推理: 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.出于多种原因 - 最次要的原因之一是它允许您从 XAML 正确绑定。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM