In WPF, I've created 3 UserControls "User Configuration", "System Configuration" & "Account Configuration". All these user controls have "Save" & "Cancel" buttons. On click of these buttons they raise a Routed Event declared and defined in their respective classes. On clicking the Save button "ConfigurationSaved" event is raised & on Cancel button "ConfigurationCancelled" event is raised.
These events when raised, the container which hosts the user control will take care of saving the configuration.
Code Snippet for all the class's routed event definition is as follows:
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
}
When I'm using these classes I'm getting TypeInitializationException with the message:
RoutedEvent Name 'ConfigurationSaved' for OwnerType 'baskcode.Admin.Controls.AccountConfigurationView' already used.
Same exception is thrown if I try to load any of the other controls. I'm not able to rectify the problem. Please help me in this regard.
I'm using .Net version 4
Thanks.
I assume that you are initializing your control more than once. You can't register the same routed event name more than once.
Try this (for one
RoutedEvent
):And this (for multiple instanaces) : in your case