2016-06-18 6 views
1

MVVMLight를 사용하여 애플리케이션을 생성합니다. 나는 내 selectedCatalogItems를 보낼 메시지를 사용하는 계획앱 시작시 ViewModel 인스턴스 지정

public class ViewModelLocator 
{ 

    public ViewModelLocator() 
    { 
     ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default); 


     SimpleIoc.Default.Register<MainViewModel>(); 
     SimpleIoc.Default.Register<CatalogViewModel>(); 
     SimpleIoc.Default.Register<CreatorViewModel>(); 
     SimpleIoc.Default.Register<DownloadsViewModel>(); 
     Messenger.Default.Register<NotificationMessage>(this, NotifyUserMethod); 
    } 


    public MainViewModel Main 
    { 
     get 
     { 
      return ServiceLocator.Current.GetInstance<MainViewModel>(); 
     } 
    } 

    public CatalogViewModel Catalog 
    { 
     get 
     { 
      return ServiceLocator.Current.GetInstance<CatalogViewModel>(); 
     } 
    } 

    public DownloadsViewModel Downloads 
    { 
     get 
     { 
      return ServiceLocator.Current.GetInstance<DownloadsViewModel>(); 
     } 
    } 

    public CreatorViewModel Creator 
    { 
     get 
     { 
      return ServiceLocator.Current.GetInstance<CreatorViewModel>(); 
     } 
    }  

    private void NotifyUserMethod(NotificationMessage message) 
    { 
     MessageBox.Show(message.Notification); 
    } 

    public static void Cleanup() 
    { 
     // TODO Clear the ViewModels 

    } 



} 

: 내 응용 프로그램에서

나는 "카탈로그"보기와 Downloads보기 ViewModelLocator에 선언되어있다 그것의 자신의 뷰 모델과 연관된 각 하나가 컬렉션에 저장되지만 사용자가 처음으로 다운로드보기를 연 경우에만 작동합니다. 다른 경우에는 다운로드 뷰 모델이 아직 생성되지 않았고 메시지가 아무 곳에도 표시되지 않습니다.

앱 시작시 Downdload VM의 생성자를 호출하는 방법이 있습니까? 아니면 다운로드 클래스를 전용 클래스로 저장해야합니까?

+0

앱의 수명 동안 뷰 모델의 인스턴스를 등록 할 수도 있습니다. 그런 식으로 인스턴스를 호출하면 매번 같은 뷰 모델을 얻게됩니다. – Nkosi

+0

훌륭한 사운드! 그것을 성취하는 방법에 대한 조언이 있습니까? 뷰 모델 인스턴스를 처리하는 mvvm 라이트에 의존하기 때문에 뷰 모델의 생성자를 어디서 어떻게 호출해야 하는지를 파악할 수 없습니다. –

+0

다른보기 모델을 등록 할 때도 같은 위치에 있습니다. viewmodel locator에 정적 생성자가 있습니까? 당신의 조각은 오히려 관절을 삐게 그래서 그것은 정적이지 그래서 MVVM 빛에서 기본적으로 동일 – Nkosi

답변

1

서비스 로케이터가 캐시의 인스턴스에 보관되므로 응용 프로그램 수명주기의 초기에 뷰 모델의 인스턴스를 가져옵니다.

public class ViewModelLocator { 
    static ViewModelLocator() { 

     SimpleIoc.Default.Register<MainViewModel>(); 
     SimpleIoc.Default.Register<CatalogViewModel>(); 
     SimpleIoc.Default.Register<CreatorViewModel>(); 
     SimpleIoc.Default.Register<DownloadsViewModel>(); 

     ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default); 

     //Default instance 
     //Done so an instance will be generated and held in cache 
     var defaultDownloadsViewModel = ServiceLocator.Current.GetInstance<DownloadsViewModel>(); 
    } 

    public ViewModelLocator(){ 
     Messenger.Default.Register<NotificationMessage>(this, NotifyUserMethod); 
    } 

    public MainViewModel Main 
    { 
     get 
     { 
      return ServiceLocator.Current.GetInstance<MainViewModel>(); 
     } 
    } 

    public CatalogViewModel Catalog 
    { 
     get 
     { 
      return ServiceLocator.Current.GetInstance<CatalogViewModel>(); 
     } 
    } 

    public DownloadsViewModel Downloads 
    { 
     get 
     { 
      return ServiceLocator.Current.GetInstance<DownloadsViewModel>(); 
     } 
    } 

    private void NotifyUserMethod(NotificationMessage message) 
    { 
     MessageBox.Show(message.Notification); 
    } 

    public static void Cleanup() 
    { 
     // TODO Clear the ViewModels 

    } 
} 
+0

완벽하게 작동합니다! 감사 ! –

+0

도와 드리겠습니다. 답변이 유용하다면 투표를 할 수 있습니다. 감사. 해피 코딩 !!! – Nkosi