我该如何举办此活动? [英] How Can I Make This Event?

查看:55
本文介绍了我该如何举办此活动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在C#中创建一个事件,当我点击一个按钮更改实心画笔颜色?(我使用画布)

how can i make an event in C# that when i click on a button change the solid brush color?(i use from a canvas)

推荐答案

你不能make a event,因为此事件实例已存在于类 Button 的每个实例中。它被称为 Button.Click 。你不需要制作它,你需要处理它。这意味着:将事件处理添加到事件实例的调用列表中,该事件实例是类 Button 的实例(非静态)事件成员: Button.Click 。使用+ =运算符添加处理程序。在XAML中可以使用设计器或不使用设计器来完成等效的事情。可以在此处找到XAML示例: hps =https ://msdn.microsoft.com/en-us/library/system.windows.controls.primitives.buttonbase.click%28v=vs.110%29.aspxtarget =_ blanktitle =New Window> ^ ]。



如果你在代码中这样做,语法是

You cannot "make an event", because this event instance already exist in each instance of the class Button. It is called Button.Click. You don't need to "make" it, you need to handle it. That means: adding an event handling to an invocation list of an event instance, which is the instance (not-static) event member of the class Button: Button.Click. The handler is added using the += operator. Equivalent thing can be done in XAML, using the designer or not. The XAML sample can be found here: https://msdn.microsoft.com/en-us/library/system.windows.controls.primitives.buttonbase.click%28v=vs.110%29.aspx[^].

If you do it in code, the syntax is
myButton.Click += MyClickHander;
// where MyClickHander is the name of the method of the signature:
// public delegate void RoutedEventHandler(
//	Object sender,
//	RoutedEventArgs e
//)
// the method itself should better be private
// for example:
//
void MyClickHander(Object sender, RoutedEventArgs e) {
      // change you color here
}

请参阅:

https://msdn.microsoft.com/en-us/library/system.windows.controls。 primitives.buttonbase.click%28v = vs.110%29.aspx [ ^ ],

https://msdn.microsoft.com/en-us/library/system.windows.routedeventhandler% 28v = vs.110%29.aspx [ ^ ],

https://msdn.microsoft.com/en-us/library/system.win dows.routedeventargs%28v = vs.110%29.aspx [ ^ ]。



注1 : WPF中的事件机制非常特殊( https://msdn.microsoft。 com / zh-CN / library / ms753115%28v = vs.110%29.aspx [ ^ ]),但在我们讨论的简单案例中,您不需要了解WPF的任何特定内容,它将是只需遵循.NET中使用的所有事件处理所使用的模式。



注2 :有些开发人员可能会说我的第一行我的代码示例在顶部是错误的,因为新的新的RoutedEventHandler(MyClickHander)而不仅仅是 MyClickHander 不要相信他们;这不是必需的。这个新是一个伪造,可能源于一些Microsoft文档的不完善之处。这与 int a = new int(); 具有正确语法的绝对冗余构造完全相同。



但我使用更简单的方法,甚至不需要知道事件参数类型(在这种简单的情况下)。这是通过使用匿名方法来完成的,尤其是在 lambda语法中:

Please see:
https://msdn.microsoft.com/en-us/library/system.windows.controls.primitives.buttonbase.click%28v=vs.110%29.aspx[^],
https://msdn.microsoft.com/en-us/library/system.windows.routedeventhandler%28v=vs.110%29.aspx[^],
https://msdn.microsoft.com/en-us/library/system.windows.routedeventargs%28v=vs.110%29.aspx[^].

Note 1: Event mechanism in WPF is very special (https://msdn.microsoft.com/en-us/library/ms753115%28v=vs.110%29.aspx[^]), but in the simple cases we are discussing you don't need to understand anything specific to WPF, it will be enough just follow the patter used for all event handling used in .NET.

Note 2: Some developers may say that my first line in my code sample on top is wrong, because new new RoutedEventHandler(MyClickHander) instead of just MyClickHander. Don't trust them; this is not needed. This "new" is a bogus which probably stems from the imperfections of some Microsoft documentation. This exactly the same kind of bogus as int a = new int(); absolutely redundant construct with correct syntax.

But I use even simpler approach which does not even require knowing the event argument types (in such simple cases). This is done with the use of anonymous methods, especially in lambda syntax:

myButton.Click += (sender, eventArgs) => { SomeCodeHandlingThisEvent(); }



即使你需要使用特定的代码 eventArgs 类型(例如,当需要时您需要获取鼠标坐标,在许多其他情况下),您不需要通过类型推断的机制来编写其类型,即推断。 />


-SA


Even if you need to use code specific the particular eventArgs type (this is needed, for example, when you need to get mouse coordinates, and in many other cases), you don't need to write its type, which is inferred through the mechanism of type inference.

—SA


这篇关于我该如何举办此活动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆