我有UserControl
作为自定义按钮,但由于我更改了路由点击事件周围的名称空间不再有效。基本上我的命名空间看起来像这样:
UI
UI.Controls
UI.Pages
我有一个UI.Controls.CustomButton
,其路由点击事件的实现类似于:
public static readonly RoutedEvent ClickEvent = EventManager.RegisterRoutedEvent(
"Click",
RoutingStrategy.Bubble,
typeof(RoutedEventHandler),
typeof(CustomButton));
public event RoutedEventHandler Click
{
add { AddHandler(ClickEvent, value); }
remove { RemoveHandler(ClickEvent, value); }
}
private void Border_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
Border_MouseEnter(sender, e);
RaiseClickEvent();
}
private void RaiseClickEvent()
{
RoutedEventArgs newEventArgs = new RoutedEventArgs(Button.ClickEvent);
RaiseEvent(newEventArgs);
}
我正试图在UI.Pages.SomePage
之内使用它:
<controls:CustomButton Text="New..." Click="NewButton_Click"/>
public void NewButton_Click(object sender, RoutedEventArgs e)
{
//do something here
}
当我调试应用程序时,我发现RaiseEvent
内的UI.Controls.CustomButton
被点击了,但我的NewButton_Click
内的UI.Pages.SomePage
从未被调用过。
当一切都在同一名称空间中时,这是有效的......我错过了一些明显的东西吗?谢谢!
答案 0 :(得分:8)
RoutedEventArgs newEventArgs = new RoutedEventArgs(Button.ClickEvent);
应该是:
RoutedEventArgs newEventArgs = new RoutedEventArgs(CustomButton.ClickEvent);