5

そのため、従来の MVVM の例では、View Models を Views にマップするために DataTemplate 定義が使用されています。MVVM Light フレームワークでこれを行う標準的な方法は何ですか?マッピングはどこに配置する必要がありますか? 以下は、私が現在行っていることと私が話していることの例です。ブレンド可能性は私にとって重要です!

メイン ウィンドウ:

<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
        mc:Ignorable="d" 
        x:Class="STS2Editor.MainWindow"
        Title="{Binding ApplicationTitle, Mode=OneWay}"
        DataContext="{Binding RootViewModel, Source={StaticResource Locator}}">
    <Window.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Skins/ApplicationSkin.xaml" />
                <ResourceDictionary Source="Resources/ViewMappings.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Window.Resources>
    <Grid>
        <ContentControl Content="{Binding ApplicationManagementViewModel}" HorizontalAlignment="Left" VerticalAlignment="Top"/>
    </Grid> 
</Window>

上記のコードでは、私の RootViewModel クラスには、同じプロパティ名を持つクラス ApplicationManagementViewModel のインスタンスがあります。

public ApplicationManagementViewModel ApplicationManagementViewModel {get {...} set {...} }

ResourceDictionary "ViewMappings.xaml" を参照して、ビュー モデルをビューとして表現する方法を指定します。

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:local="clr-namespace:STS2Editor.ViewModel">
    <DataTemplate DataType="{x:Type local:ApplicationManagementViewModel}">
        <local:ApplicationManagementView/>
    </DataTemplate>
</ResourceDictionary>

ViewModelLocator を使用してこのようなことを行う必要がありますか? ビューモデルのコレクションはどうですか?

4

1 に答える 1

4

使用するメソッド(暗黙的に型指定されたDataTemplatesを使用)はWPFでは正常に機能しますが、残念ながらSilverlightでは機能しません。これが、両方の世界で機能するより明示的な方法を使用することを好む理由の1つです。

また、暗黙的に入力されたDataTemplatesは、テンプレートがどこから来たのかが常に明確であるとは限らないため、少し混乱する可能性があります。これにより、インテグレーターの作業が非常に困難になる場合があります。特に、UIに小さな変更を加えた場合は、非常に困難になります(そこで、それを実行します:)

MVVM LightでViewModelLocatorを使用する義務はありません。これは、うまく機能し、非常に理解しやすい方法です(WPF / SLの微妙な点に精通していないコードを読んでいる人にとって)。結局、それは非常に好みの問題ですが、最近、ViewModelLocatorパターンが人気を博しているようです(たとえば、一般的なViewModelLocatorがMEFと一緒に使用されるこの投稿を参照してください)。

http://www.johnpapa.net/simple-viewmodel-locator-for-mvvm-the-patients-have-left-the-asylum/

最後に、MVVM LightでのViewModelLocatorの現在の実装にはあまり満足していないことを付け加えておきます。次のバージョンでは、はるかに一般的なソリューションを提案したいと思います。

于 2010-04-18T18:49:17.910 に答える