如果有人能用简单的例子来解释一下,我会很感激。
public class MyClassWithARoutedEvent : UIElement
{
public static readonly RoutedEvent DoSomethingEvent;
public event RoutedEventHandler DoSomething
{
add { base.AddHandler ( MyClassWithARoutedEvent.DoSomethingEvent, value );
remove { base.AddHandler ( MyClassWithARoutedEvent.DoSomethingEvent, value );
}
}
在这种情况下,您通常会使用宣传活动:
此外,值得一提的是,即使您的控件没有定义RoutedEvent或继承自元素,您仍然可以在其中使用RoutedEvent。这是因为您可以真正将RoutedEvent实例视为事件的强类型名称。因此,如果您可以访问此“名称”(这就是为什么路由事件的实例通常是公开的原因),您可以拥有它:
public class MyClassWithARoutedEvent : UIElement
{
public static readonly RoutedEvent ClickEvent;
static MyClassWithARoutedEvent ( )
{
ClickEvent = ButtonBase.ClickEvent.AddOwner( typeof ( MyClassWithARoutedEvent ) );
}
// A wrapper should be placed here as described above
}