在WPF中,我創建了3個UserControls「用戶配置」,「系統配置」&「帳戶配置」。所有這些用戶控件都具有「保存」&「取消」按鈕。點擊這些按鈕後,他們會在各自的類中聲明和定義路由事件。點擊保存按鈕「ConfigurationSaved」事件引發&上的取消按鈕「ConfigurationCancelled」事件引發。RoutedEvent Name已使用
這些事件引發時,容納用戶控件的容器將負責保存配置。
代碼段爲所有類的路由事件的定義如下:
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
感謝。