2016-06-18 8 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 

    } 



} 

:私はViewModelLocatorで宣言されているそれ自身のViewModelに関連する各1「カタログ」ビューとDownloadsビューを持っている私のアプリで

コレクションのダウンロードVMに含まれていますが、ユーザーが最初にダウンロードビューを開いた場合にのみ機能します。それ以外の場合、ダウンロードビューモデルはまだ作成されておらず、メッセージはどこにもありません。

アップロード時にDowndload VMのコンストラクタを呼び出す方法がありますか、または専用のクラスを使用してダウンロードリストを保存する必要がありますか?

+0

また、アプリの存続期間中にビューモデルのインスタンスを登録することもできます。そのようにしてインスタンスを取得すると、毎回同じビューモデルが取得されます。 – Nkosi

+0

いいですね!それを達成するためのアドバイスはありますか?ビューモデルのインスタンス化を処理するmvvmライトに依存するため、ビューモデルのコンストラクタをどこでどのように呼び出すべきかを把握することはできません。 –

+0

他のビューモデルを登録するときも同じです。 viewmodelロケータは静的コンストラクタを持っていますか?スニペットはかなりバラバラですので、それは静的ではありませんので、ここで – 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

関連する問題