【发布时间】:2021-11-04 07:59:12
【问题描述】:
我有一个简单的用户控件,在我的用户控件中我有两个这样的按钮
<Button Name="elDownStepTgBtn" Click="HandleButtonClick1" ></Button>
<Button Name="elUpStepTgBtn" Click="HandleButtonClick2" ></Button>
在后面的代码中,我为按钮实现了两个路由事件:decreaseEvent 和 increaseEvent,
public partial class UpDownUc :UserControl
{
public UpDownUc()
{
InitializeComponent();
}
// Create RoutedEvent
// This creates a static property on the UserControl, SettingsConfirmedEvent, which
// will be used by the Window, or any control up the Visual Tree, that wants to
// handle the event. This is the Custom Routed Event for more info on the
// RegisterRoutedEvent method
// https://msdn.microsoft.com/en-us/library/ms597876(v=vs.100).aspx
public static readonly RoutedEvent decreaseEvent =
EventManager.RegisterRoutedEvent("SettingDecreaseEvent", RoutingStrategy.Bubble,
typeof(RoutedEventHandler), typeof(UpDownUc));
public static readonly RoutedEvent increaseEvent =
EventManager.RegisterRoutedEvent("SettingIncreasaeEvent", RoutingStrategy.Bubble,
typeof(RoutedEventHandler), typeof(UpDownUc));
// Create RoutedEventHandler
// This adds the Custom Routed Event to the WPF Event System and allows it to be
// accessed as a property from within xaml if you so desire
public event RoutedEventHandler SettingDecrease
{
add { AddHandler(decreaseEvent, value); }
remove { RemoveHandler(decreaseEvent, value); }
}
public event RoutedEventHandler SettingIncrease
{
add { AddHandler(increaseEvent, value); }
remove { RemoveHandler(increaseEvent, value); }
}
// When the Save button on the User Control is clicked, use RaiseEvent to fire the
// Custom Routed Event
private void HandleButtonClick1(object sender, RoutedEventArgs e)
{
// Raise the custom routed event, this fires the event from the UserControl
RaiseEvent(new RoutedEventArgs(UpDownUc.decreaseEvent));
}
private void HandleButtonClick2(object sender, RoutedEventArgs e)
{
// Raise the custom routed event, this fires the event from the UserControl
RaiseEvent(new RoutedEventArgs(UpDownUc.increaseEvent));
}
}
我在MainWindow.xaml中使用这个用户控件
<local:UpDownUc x:Name="elevUpDown" ></local:UpDownUc>
并在 MainWindow.cs 中注册事件
// Register the Bubble Event Handler
AddHandler(UpDownUc.decreaseEvent, new RoutedEventHandler(elDownStepTgBtn_MouseLeftButtonDown));
//i need to create another instance but if use the code below ,rises events are the same
AddHandler(azUpDown.increaseEvent, new RoutedEventHandler(elUpStepTgBtn_MouseLeftButtonDown));
实现
private void elDownStepTgBtn_MouseLeftButtonDown(object sender, RoutedEventArgs e)
{
UpDownUc.vm.val = Math.Round(UpDownUc.vm.val - Parameters.upDownStepVal, 2);
}
private void elUpStepTgBtn_MouseLeftButtonDown(object sender, RoutedEventArgs e)
{
UpDownUc.vm.val = Math.Round(UpDownUc.vm.val + Parameters.upDownStepVal, 2);
}
这可以正常工作,但问题是:当我创建新实例时发生异常
the routed evnt named SettingDecreaseEvent for ownerType UpDownUc already used
如果我是认真的。我只是想重用一个已经实现路由事件的用户控件。 谁能给我解释一下发生了什么,非常感谢
【问题讨论】:
-
@Clemens 但是当我改用“静态”时,我必须通过主窗口中的类名访问用户控件事件(当我注册事件时),我想按名称访问用户控件,因为我有两个实例
-
public readonly RoutedEvent ...必须是public static readonly RoutedEvent ...。见How to: Create a Custom Routed Event。还有一个命名约定,字段应命名为SettingDecreaseEvent和SettingIncreaseEvent。最后,使用nameof(SettingDecrease)和nameof(SettingIncrease)来避免像"SettingIncreasaeEvent"这样的印刷错误——应该是"SettingIncrease"。 -
除此之外,您只能访问非静态 SettingDecrease 和 SettingIncrease 事件,而不是它们的静态支持字段:
elevUpDown.SettingDecrease += elDownStepTgBtn_MouseLeftButtonDown; -
@Clemens,我阅读了链接并纠正了我的错误,但问题仍然存在,我需要在我的主窗口中使用具有不同功能的这个用户控件的几个实例,谢谢
-
不确定是什么阻止了您使用多个实例。也许编辑问题并向我们展示您当前的代码。