2011-02-01 12 views
1

MEF를 사용하여 내 뷰 모델 클래스의 인터페이스로 가져 오기 클래스에 문제가 있습니다. MEF 가져 오기 관련 문제

나는 모델을 내가 만들어 볼 재산 :

[Import] 
    private IEventAggregator EventAgg { get; set; } 

내가보기 모델의 생성자에서이 이벤트 애그리 게이터 (aggregator)를 subcribe 원하는 :

public MessengerViewModel() 
    { 
     EventAgg.Subscribe(this); 
    } 

내가 응용 프로그램을 실행 :

그래서 나는이 시도 하지만 애플 리케이션 동결.

그래서 나는 OnImportsSatisfied 방법으로 이벤트 애그리 게이터 (aggregator)를 등록하려고 :

public void OnImportsSatisfied() 
    { 
     EventAgg.Subscribe(this); 
    } 

그러나 뷰 모델 클래스가 생성 될 때이 방법은 결코 호출되지 않습니다. 작동하는

유일한 해결책은 다음과 같습니다

[ImportingConstructor] 
    public MessengerViewModel(IEventAggregator eventAggregator) 
    { 
     EventAgg = eventAggregator; 
     EventAgg.Subscribe(this); 
    } 

처음 두 가지 방법으로 나쁜 무엇입니까?

답변

1

첫 번째 방법은 작동하지 않습니다 호출하려는 경우 당신은 당신의 ViewModel에 IPartImportsSatisfiedNotification를 구현해야

  • 전용 속성에 속성 (세터) 주입을 사용할 수 없습니다 생성자가 호출 될 때까지 EventAgg 종속성이 여전히 null이기 때문입니다.

    OnImportsSatisfied의 두 번째 방법은보기 모델이 IPartImportsSatisfiedNotification imterface를 구현하는 한 작동해야합니다.

  • 2
    1. 당신은 MEF가 자동으로 OnImportsSatisfied는