在WPF中,我创建了3个UserControls“User Configuration”,“System Configuration”& “帐户配置”。所有这些用户控件都具有“保存”功能。 “取消”按钮。单击这些按钮,它们会引发在各自类中声明和定义的路由事件。点击“保存”按钮后,会出现“ConfigurationSaved”事件。在取消按钮上,“ConfigurationCancelled”事件被引发。
这些事件在引发时,承载用户控件的容器将负责保存配置。
所有类的路由事件定义的代码片段如下:
AccountConfigurationView:
public partial class AccountConfigurationView : UserControl
{
static AccountConfigurationView()
{
ConfigurationSavedEvent = EventManager.RegisterRoutedEvent("ConfigurationSaved",
RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(AccountConfigurationView));
ConfigurationClosedEvent = EventManager.RegisterRoutedEvent("ConfigurationClosed",
RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(AccountConfigurationView));
}
#region ROUTED_EVENTS_RELATED
public static readonly RoutedEvent ConfigurationSavedEvent;
public static readonly RoutedEvent ConfigurationClosedEvent;
public event RoutedEventHandler ConfigurationSaved
{
add { AddHandler(ConfigurationSavedEvent, value); }
remove { RemoveHandler(ConfigurationSavedEvent, value); }
}
public event RoutedEventHandler ConfigurationClosed
{
add { AddHandler(ConfigurationClosedEvent, value); }
remove { RemoveHandler(ConfigurationClosedEvent, value); }
}
#endregion
}
SystemConfigurationView:
public partial class SystemConfigurationView : UserControl
{
static SystemConfigurationView()
{
ConfigurationSavedEvent = EventManager.RegisterRoutedEvent("ConfigurationSaved",
RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(SystemConfigurationView));
ConfigurationClosedEvent = EventManager.RegisterRoutedEvent("ConfigurationClosed",
RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(SystemConfigurationView));
}
#region ROUTED_EVENTS_RELATED
public static readonly RoutedEvent ConfigurationSavedEvent;
public static readonly RoutedEvent ConfigurationClosedEvent;
public event RoutedEventHandler ConfigurationSaved
{
add { AddHandler(ConfigurationSavedEvent, value); }
remove { RemoveHandler(ConfigurationSavedEvent, value); }
}
public event RoutedEventHandler ConfigurationClosed
{
add { AddHandler(ConfigurationClosedEvent, value); }
remove { RemoveHandler(ConfigurationClosedEvent, value); }
}
#endregion
}
UserConfigurationView:
public partial class UserConfigurationView : UserControl
{
static UserConfigurationView()
{
ConfigurationSavedEvent = EventManager.RegisterRoutedEvent("ConfigurationSaved",
RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(UserConfigurationView));
ConfigurationClosedEvent = EventManager.RegisterRoutedEvent("ConfigurationClosed",
RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(UserConfigurationView));
}
#region ROUTED_EVENTS_RELATED
public static readonly RoutedEvent ConfigurationSavedEvent;
public static readonly RoutedEvent ConfigurationClosedEvent;
public event RoutedEventHandler ConfigurationSaved
{
add { AddHandler(ConfigurationSavedEvent, value); }
remove { RemoveHandler(ConfigurationSavedEvent, value); }
}
public event RoutedEventHandler ConfigurationClosed
{
add { AddHandler(ConfigurationClosedEvent, value); }
remove { RemoveHandler(ConfigurationClosedEvent, value); }
}
#endregion
}
当我使用这些类时,我收到TypeInitializationException并显示消息:
RoutedEvent名称'ConfigurationSaved'用于OwnerType'baskcode.Admin.Controls.AccountConfigurationView'已经使用过。
如果我尝试加载任何其他控件,则抛出相同的异常。我无法纠正这个问题。请帮助我。
我正在使用 .Net版本4
感谢。
答案 0 :(得分:1)
我假设你不止一次初始化你的控件。您不能多次注册相同的路由事件名称。
尝试此操作(对于一个RoutedEvent
):
RoutedEvent[] x = EventManager.GetRoutedEventsForOwner(typeof(UserConfigurationView));
if(x == null) {
ConfigurationSavedEvent = EventManager.RegisterRoutedEvent(
"ConfigurationSaved",
RoutingStrategy.Bubble,
typeof(RoutedEventHandler),
typeof(UserConfigurationView)
);
}
这个(对于多个实例):在你的情况下
using System.Linq;
...
RoutedEvent[] x = EventManager.GetRoutedEvents();
if(!x.Contains(ConfigurationSaved)) {
ConfigurationSavedEvent = EventManager.RegisterRoutedEvent(
"ConfigurationSaved",
RoutingStrategy.Bubble,
typeof(RoutedEventHandler),
typeof(UserConfigurationView)
);
}
答案 1 :(得分:1)
检查EventManager.RegisterRoutedEvent参数。如果您已从其他文件复制并粘贴它,则很容易忘记更改所有者类型。例如:
public class UserControlA
{
public static readonly RoutedEvent ClickEvent =
EventManager.RegisterRoutedEvent(
"Click",
RoutingStrategy.Bubble,
typeof(RoutedEventHandler),
typeof(UserControlA));
}
然后你将它复制粘贴到另一个文件。请注意下面错误的 typeof(UserControlA),它应该是 typeof(UserControlB)。
public class UserControlB
{
public static readonly RoutedEvent ClickEvent =
EventManager.RegisterRoutedEvent(
"Click",
RoutingStrategy.Bubble,
typeof(RoutedEventHandler),
typeof(UserControlA));
}