简单的定义下面的路由事件类,死东西,敲熟练
public class MyRoutedEvents : UIElement { public static readonly RoutedEvent ButtonClickEvent = EventManager.RegisterRoutedEvent("ButtonClick", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(MyRoutedEvents)); public event RoutedEventHandler ButtonClick { add { AddHandler(ButtonClickEvent, value); } remove { RemoveHandler(ButtonClickEvent, value); } } public void SendEvent(RoutedEvent evt, RoutedEventArgs e) { e.RoutedEvent = evt; RaiseEvent(e); } public virtual void OnButtonClick() { SendEvent(ButtonClickEvent, new RoutedEventArgs()); } }
前台需要一个控件的事件来激活
private void img__MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { MyRoutedEvents routed = new MyRoutedEvents(); routed.ButtonClick += new RoutedEventHandler((object sender2, RoutedEventArgs evte) => { MessageBox.Show("Clicked me."); }); routed.OnButtonClick(); }
ok了。