2011-02-17 23 views
0

在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

感謝。

回答

1

我假設你正在初始化你的控件多次。您不能多次註冊相同的路由事件名稱。

試試這個(一RoutedEvent):

RoutedEvent[] x = EventManager.GetRoutedEventsForOwner(typeof(UserConfigurationView)); 

if(x == null) { 
    ConfigurationSavedEvent = EventManager.RegisterRoutedEvent(
     "ConfigurationSaved", 
     RoutingStrategy.Bubble, 
     typeof(RoutedEventHandler), 
     typeof(UserConfigurationView) 
    ); 
} 

而這(對於多instanaces):你的情況

using System.Linq; 

... 

RoutedEvent[] x = EventManager.GetRoutedEvents(); 

if(!x.Contains(ConfigurationSaved)) { 
    ConfigurationSavedEvent = EventManager.RegisterRoutedEvent(
     "ConfigurationSaved", 
     RoutingStrategy.Bubble, 
     typeof(RoutedEventHandler), 
     typeof(UserConfigurationView) 
    ); 
} 
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)); 
} 
相關問題