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의 생성자를 호출하는 방법이 있습니까? 아니면 다운로드 클래스를 전용 클래스로 저장해야합니까?
앱의 수명 동안 뷰 모델의 인스턴스를 등록 할 수도 있습니다. 그런 식으로 인스턴스를 호출하면 매번 같은 뷰 모델을 얻게됩니다. – Nkosi
훌륭한 사운드! 그것을 성취하는 방법에 대한 조언이 있습니까? 뷰 모델 인스턴스를 처리하는 mvvm 라이트에 의존하기 때문에 뷰 모델의 생성자를 어디서 어떻게 호출해야 하는지를 파악할 수 없습니다. –
다른보기 모델을 등록 할 때도 같은 위치에 있습니다. viewmodel locator에 정적 생성자가 있습니까? 당신의 조각은 오히려 관절을 삐게 그래서 그것은 정적이지 그래서 MVVM 빛에서 기본적으로 동일 – Nkosi