创建RoutedEventargs的子类以保存附加数据,并使用
EventHandler<T>
你的args类。这将转换为routedeventhandler,您的处理程序中将提供其他数据。
您可以创建一个包含任何类型的单个参数的通用RoutedEventargs类,但是创建一个新类通常会使代码更容易读取,并且更容易修改,以便将来包含更多参数。
public class FooEventArgs
: RoutedEventArgs
{
// Declare additional data to pass here
public string Data { get; set; }
}
public class FooControl
: UserControl
{
public static readonly RoutedEvent FooEvent =
EventManager.RegisterRoutedEvent("Foo", RoutingStrategy.Bubble,
typeof(EventHandler<FooEventArgs>), typeof(FooControl));
public event EventHandler<FooEventArgs> Foo
{
add { AddHandler(FooEvent, value); }
remove { RemoveHandler(FooEvent, value); }
}
protected void OnFoo()
{
base.RaiseEvent(new FooEventArgs()
{
RoutedEvent = FooEvent,
// Supply the data here
Data = "data",
});
}
}