在WPF中,我创建了3个UserControls“用户配置”、“系统配置”和“帐户配置”。所有这些用户控件都有“保存”和“取消”按钮。在单击这些按钮时,它们会引发在各自类中声明和定义的路由事件。单击“保存”按钮会引发"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:
用于'baskcode.Admin.Controls.AccountConfigurationView‘的
RoutedEvent名称“ConfigurationSaved”已经使用过。
如果我试图加载任何其他控件,也会引发相同的异常。我无法纠正这个问题。请在这方面帮助我。
我使用的是.Net版本4
谢谢。
发布于 2011-05-21 16:38:21
我假设您正在多次初始化您的控件。不能多次注册相同的路由事件名。
试试这个(对于一个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)
);
}发布于 2013-02-17 17:48:21
检查您的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));
}https://stackoverflow.com/questions/5028419
复制相似问题