0

私はMVVMを実装しようとしているので、以下が正しいかわかりません。ViewModel はある種のビューのモデルのように見えるため、ビュー内の関連付けは ViewModel に表示されます。その場合、ViewModel 間にいくつかの関連付けが必要です。したがって、ViewModel 型のテンプレートをいくつか作成することで、アプリケーションが機能するように見えます。コード例を次に示します。

ビューモデル:

public class SomeVm : INotifyPropertyChanged
{
    public SomeVm()
    {
        SomeOtherVm = new SomeOtherVm();
    }
    public INotifyPropertyChanged SomeOtherVm { set; get; }

    private int _a;
    public int A
    {
        set { 
            _a= value;
            B = value;
        }
        get { return _a; }
    }

    private int _b;
    public int B
    {
        set 
        {
            _b = value;
            OnPropertyChanged("B");
        }
        get { return _b; }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged(string propertyName)
    {
        var handler = PropertyChanged;
        if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
    }
}

public class SomeOtherVm : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    private int _c;
    public int C
    {
        set
        {
            _c = value;
            D = value;
        }
        get { return _c; }
    }

    private int _d;
    public int D
    {
        set
        {
            _d = value;
            OnPropertyChanged("D");
        }
        get { return _d; }
    }
    protected virtual void OnPropertyChanged(string propertyName)
    {
        var handler = PropertyChanged;
        if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
    }
}

そしてビュー:

<Window
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:wpfApplication1="clr-namespace:WpfApplication1"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
        mc:Ignorable="d" 
        x:Class="WpfApplication1.MainWindow"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <wpfApplication1:SomeVm x:Key="SomeVm"/>
        <DataTemplate DataType="{x:Type wpfApplication1:SomeVm}">
            <StackPanel d:DesignWidth="339" d:DesignHeight="54">
                <TextBox HorizontalAlignment="Left" TextWrapping="Wrap" Text="{Binding A}" VerticalAlignment="Stretch"/>
                <TextBlock HorizontalAlignment="Left" TextWrapping="Wrap" Text="{Binding B}" VerticalAlignment="Stretch"/>
                <ContentPresenter Content="{Binding SomeOtherVm}"/>
            </StackPanel>

        </DataTemplate>
        <DataTemplate DataType="{x:Type wpfApplication1:SomeOtherVm}">
            <StackPanel d:DesignWidth="339" d:DesignHeight="54">
                <TextBox HorizontalAlignment="Left" TextWrapping="Wrap" Text="{Binding C}" VerticalAlignment="Stretch"/>
                <TextBlock HorizontalAlignment="Left" TextWrapping="Wrap" Text="{Binding D}" VerticalAlignment="Stretch"/>
            </StackPanel>
        </DataTemplate>

    </Window.Resources>
    <Grid>
        <ContentPresenter Content="{DynamicResource SomeVm}" />   
    </Grid>
</Window>

このようにして、すべてのビューをいくつかのリソース ディクショナリで作成できるので、問題は次のとおりです。このように MVVM を使用するのは正しいでしょうか? もしそうなら、欠点は何ですか?

4

2 に答える 2

1

通常、ViewModel はビュー全体の DataContext であると想定されています。つまり、ビューにデータを提供してそれ自体をレンダリングし、UI コマンド、イベント、およびプロパティの変更をリッスンしてビジネス層 (モデル) と対話するエンティティである必要があります。

あなたがそれを実装した方法は、リソースとして VM を持ち、提示された 1 つのコンテンツの DataContext ではなく、それをコンテンツとして設定することです。ただし、VM をビュー全体の DataContext として設定して、ビュー内のすべての要素を VM 内のプロパティにバインドしてその状態をレンダリングできるようにする必要があります。

シーンで、ContentPresenter とは別にビューに UI 要素をもう 1 つ追加する必要がある場合は、リソース VM にアクセスする必要があります。

したがって、VM インスタンスを DataContext (this.DataContext = new ViewModel() など) として設定し、contentpresenter Content を Content={Binding} などのビューの DataContext にバインドすると、より正確になり、拡張したい場合に役立ちます。あなたの見解。これは、mvvm の実装に関する素晴らしい msdn の記事ですhttp://msdn.microsoft.com/en-us/library/gg405484(v=pandp.40).aspx

ありがとう

于 2013-09-07T18:51:50.413 に答える