WPF MVVMなぜストレートXAMLウィンドウビューではなく、ContentControl + DataTemplateビューを使用するのですか?

サイモンf

なぜこれ?

MainWindow.xaml:

<Window x:Class="MVVMProject.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Grid>
        <ContentControl Content="{Binding}"/>
    </Grid>
</Window>

ExampleView.xamlを次のように設定します。

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:vms="clr-namespace:MVVMProject.ViewModels">
    <DataTemplate DataType="{x:Type vms:ExampleVM}" >
        <Grid>
            <ActualContent/>
        </Grid>
    </DataTemplate>
</ResourceDictionary>

そして、次のようなウィンドウを作成します。

public partial class App : Application {

    protected override void OnStartup(StartupEventArgs e) {

        base.OnStartup(e);

        MainWindow app = new MainWindow();
        ExampleVM context = new ExampleVM();
        app.DataContext = context;
        app.Show();
    }
}

このようにできるのはいつですか?

App.xaml :(起動ウィンドウ/ビューの設定)

<Application x:Class="MVVMProject.App"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    StartupUri="ExampleView.xaml">
</Application>

ExampleView.xaml :(リソース辞書ではないウィンドウ)

<Window x:Class="MVVMProject.ExampleView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:vms="clr-namespace:MVVMProject.ViewModels">
    >
    <Window.DataContext>
        <vms:ExampleVM />
    </Window.DataContext>

    <Grid>
        <ActualContent/>
    </Grid>
</Window>

基本的には、「DataTemplateとして表示」(VaD)と「ウィンドウとして表示」(VaW)です。

これが比較についての私の理解です:

  • VaD:ウィンドウを閉じずにビューを切り替えることができます。(これは私のプロジェクトには望ましくありません)
  • VaD:VMはビューについてまったく何も知りませんが、VaWでは、別のウィンドウを開くときにVMをインスタンス化できる必要があります(のみ)
  • VaW:デザイナーでレンダリングされたxamlを実際に見ることができます(少なくとも現在のセットアップでは、VaDでは表示できません)
  • VaW:ウィンドウの開閉を直感的に操作できます。各ウィンドウには、対応するビュー(およびViewModel)があります。
  • VaD:ViewModelは、プロパティを介して初期ウィンドウの幅、高さ、サイズ変更可能性などを渡すことができます(VaWではウィンドウに直接設定されます)
  • VaW:FocusManager.FocusedElementを設定できます(VaDでの方法はわかりません)
  • VaW:ウィンドウタイプ(リボン、ダイアログなど)がビューに組み込まれているため、ファイルが少なくなります

では、ここで何が起こっているのでしょうか。XAMLでウィンドウを構築し、VMのプロパティを介してデータにクリーンにアクセスし、それを実行することはできませんか?コードビハインドは同じです(事実上ゼロ)。

すべてのViewのものをResourceDictionaryにシャッフルする必要がある理由を理解するのに苦労しています。

フェデリコベラサテギ

DataTemplatesViewModelに応じてビューを動的に切り替えたい場合は、この方法を使用します。

<Window>
    <Window.Resources>
       <DataTemplate DataType="{x:Type local:VM1}">
          <!-- View 1 Here -->
       </DataTemplate>

       <DataTemplate DataType="{x:Type local:VM2}">
          <!-- View 2 here -->
       </DataTemplate>
    </Window.Resources>

    <ContentPresenter Content="{Binding}"/>

</Window>

そう、

場合Window.DataContextのインスタンスであるVM1場合、View1表示されます、

で、もし

Window.DataContextはのインスタンスでありVM2View2が表示されます。

確かに、1つのビューのみが期待され、変更されない場合は、まったく意味がありません。

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

Related 関連記事

ホットタグ

アーカイブ