WPF MVVM为什么使用ContentControl中+ DataTemplate中浏览,而不是直XAML的窗口视图? [英] WPF MVVM Why use ContentControl + DataTemplate Views rather than straight XAML Window Views?
问题描述
我有一个关于MVVM在WPF这是推动我发疯的问题。
I have a question about MVVM in WPF that is driving me batty.
为什么做这样的事情:
MainWindow.xaml:
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设置为:
Have your ExampleView.xaml set up as:
<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中:(设置启动窗口/视图)
App.xaml: (Set startup window/View)
<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:(一窗口不是一个ResourceDictionary中)
ExampleView.xaml: (a Window not a ResourceDictionary)
<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)
下面是我比较理解:(注意:我使用VS 2008,所以我没有混合能力和放大器; /或其他的东西)
Here is my understanding of the comparison: (Note I use VS 2008 so I lack Blendability &/or other stuff)
- 痴呆:让您切换视图而不关闭窗口。 (这不是希望我的项目)
- 痴呆:VM知道绝对没有关于视图,而在VAW它(只)必须能够打开另一个窗口时对其进行实例
- VAW:其实我可以看到我的XAML在设计呈现(我不能
与痴呆,至少在我的当前设置) - VAW:用直观的工作
地打开和关闭的窗户;每个窗口都有(是)一个相应的视图
(和视图模型) - 痴呆:视图模型可以通过属性一起初始窗口宽度,高度可调整大小等传(而在VAW他们在窗口中直接设置)
- VAW:可设置FocusManager.FocusedElement(如何痴呆不知道)
- VAW:少的文件,因为我的窗口类型(如丝带,对话)被纳入了自己的看法
那么是什么回事?我不能只是建立我的窗户在XAML,访问他们的数据清晰地通过VM的性能,并用它做什么?在code-背后是一样的(几乎为零)。我努力理解我为什么要打乱所有视图东西到ResourceDictionary中。
(但我不想这样做的错误的;-))
So what's going on here? Can't I just build my windows in XAML, access their data cleanly through properties of the VM, and be done with it? The code-behind is the same (virtually nil). I'm struggling to understand why I should shuffle all the View stuff into a ResourceDictionary. ( But I don't want to do it wrong ;-) )
难道它甚至重要吗?有什么我已经错过了?
非常感谢您阅读。 :o
Does it even matter? Is there something I've missed? Thanks a lot for reading. :O
的感谢雷切尔Lim和尼克Polyak为MVVM我的理解朵朵的
编辑:小流量变化
推荐答案
人们使用的DataTemplates
时,他们要动态切换取决于视图模型视图的方式:
People use DataTemplates
that way when they want to dynamically switch Views depending on the ViewModel:
<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
的一个实例,那么视图1
将显示,
if Window.DataContext
is an instance of VM1
, then View1
will be displayed,
如果
Window.DataContext
是 VM2
的一个实例,那么视图2
将被显示。
Window.DataContext
is an instance of VM2
, then View2
will be displayed.
当然,它是没有意义的,如果只有1查看的预期,并没有改变。
Granted, it makes no sense at all if only 1 View is expected, and never changed.
我希望这是非常明显的:P
I hope this is clear enough :P
这篇关于WPF MVVM为什么使用ContentControl中+ DataTemplate中浏览,而不是直XAML的窗口视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!