2011-02-17 5 views
0

WPF에서 3 개의 UserControls "사용자 구성", "시스템 구성"& "계정 구성"을 만들었습니다. 이 모든 사용자 컨트롤에는 "저장"& "취소"버튼이 있습니다. 이 버튼을 클릭하면 각 클래스에서 선언되고 정의 된 라우트 된 이벤트가 발생합니다. 저장 버튼을 클릭하면 "ConfigurationSaved"이벤트가 발생합니다. Cancel 버튼에서 & "ConfigurationCancelled"이벤트가 발생합니다.RoutedEvent 이름이 이미 사용되었습니다.

이러한 이벤트가 발생하면 사용자 정의 컨트롤을 호스팅하는 컨테이너가 구성을 저장합니다.

다음 모든 클래스의 라우트 된 이벤트 정의에 대한

코드 조각이 될 때 :

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 이름은 이미 사용 OwnerType 'baskcode.Admin.Controls.AccountConfigurationView'에 대한 'ConfigurationSaved'.

다른 컨트롤을로드하려고하면 같은 예외가 발생합니다. 나는 문제를 바로 잡을 수 없다. 이 점에 대해 제발 도와주세요.

내가 닷넷 버전 4

감사를 사용하고 있습니다.

답변

1

컨트롤을 두 번 이상 초기화한다고 가정합니다. 동일한 라우트 된 이벤트 이름을 두 번 이상 등록 할 수 없습니다.

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

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

을 그리고이 (여러 instanaces에 대한) :

이 ( RoutedEvent 하나)를 시도

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)); 
} 
관련 문제