简体   繁体   English

如何重用事件处理程序?

[英]How do i reuse event handler?

I got a code: 我有一个代码:

private void border1_PreviewGiveFeedback(object sender, GiveFeedbackEventArgs e)
{
    // some code
}

mainwindow.xaml: mainwindow.xaml:

<Border BorderBrush="Silver" PreviewGiveFeedback="border1_PreviewGiveFeedback"
        Name="border1" />

Now i want to reuse the event handler in border1 inside border2 (without writig the same code in border2 and cs file) What to write here: 现在我想在border2内的border1中重用事件处理程序(而在border2和cs文件中不写相同的代码)在这里写什么:

<Border Name="border2" />

我认为与您在第一个边界中使用的相同

<Border PreviewGiveFeedback="border1_PreviewGiveFeedback" Name="border2" />

The PreviewGiveFeedback property is simply asking for an event handler with a certain signature. PreviewGiveFeedback属性只是请求具有特定签名的事件处理程序。 You could actually name it Potatoes_AreTasty__YUM and it would function perfectly well as long as it had the proper signature. 您实际上可以将其命名为Potatoes_AreTasty__YUM ,只要它具有适当的签名,它就可以很好地运行。

Thus, the way to use it in border2 is to just give it the same event method as border1. 因此,在border2中使用它的方法就是为它提供与border1相同的事件方法。 You could also re-name the function to indicate that it isn't exclusively for border1 but a generic border preview event handler. 您还可以重命名该函数,以指示它不是专门为border1而是一个通用的边框预览事件处理程序。

You can do exactly the same: 您可以执行完全相同的操作:

<Border Name="border2" PreviewGiveFeedback="border1_PreviewGiveFeedback" />

is going to work. 要上班了。 You should consider renaming the event handler method in that case, though. 不过,在这种情况下,您应该考虑重命名事件处理程序方法。

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

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