0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Prism コードサンプル学習:08-ViewModelLocator

Posted at

Prism コードサンプル学習:08-ViewModelLocator

はじめに

以下の記事の続きです。
https://qiita.com/mngreen/items/dedaef545e3f5122b672

08-ViewModelLocator

本サンプルではViewModelとViewの紐付けが自動で行えることを示しています。
ポイントとなるコードは以下のprism:ViewModelLocator.AutoWireViewModel="True"となります。

<Window x:Class="ViewModelLocator.Views.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:prism="http://prismlibrary.com/"
        prism:ViewModelLocator.AutoWireViewModel="True"
        Title="{Binding Title}" Height="350" Width="525">
    <Grid>
        <ContentControl prism:RegionManager.RegionName="ContentRegion" />
    </Grid>
</Window>
  • 上記の値は添付プロパティとしてここに定義されています。
    • 添付プロパティのコールバックに設定されているAutoWireViewModelChangedメソッドが呼び出されると、ViewModelLocationProvider.AutoWireViewModelChangedが呼び出されます。この中で、Viewと紐づくViewModelが取得されてDataContextに設定されます。
    • デフォルトでのView-ViewModelの紐付けはここで定義されている。
    • ViewModelLocationProvider.SetDefaultViewTypeToViewModelTypeResolverメソッドを利用すればルールを変更できそうであるが、UTでしか利用されていなかったため、後のサンプルを読み進めることで実際の利用箇所を見てみようと思います。

おわりに

今回はViewModelLocatorの読み込む方法について確認しました。
ViewとViewModelが意識することなく紐付けられるのはとても強力に感じました。
次回、09-ChangeConventionについて見ていこうと思います。

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?