定义嵌套视图的标准约定是什么:MVVM Light 中的视图模型映射
因此,在经典的 MVVM 示例中,我看到 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 做这样的事情吗?视图模型的集合怎么样?
so in classic MVVM examples ive seen DataTemplate definitions are used to map up View Models to Views, what is the standard way to do this in MVVM Light framework, and where should the mappings be located? Following are examples of what I'm doing now and what I'm talking about, blendability is important to me!
Main Window:
<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>
In the above code, my RootViewModel class has an instance of the class ApplicationManagementViewModel with the same property name:
public ApplicationManagementViewModel ApplicationManagementViewModel {get {...} set {...} }
I reference the ResourceDictionary "ViewMappings.xaml" to specify how my view model is represented as a view.
<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>
should I be doing things like this using ViewModelLocator? what about collections of view models?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您使用的方法(使用隐式类型的 DataTemplates)在 WPF 中工作正常,但不幸的是它在 Silverlight 中不起作用。这就是为什么我更喜欢使用一种在两个领域都有效的更明确的方法的原因之一。
此外,隐式类型的 DataTemplate 可能会有点令人困惑,因为模板的来源并不总是很清楚。这有时会使集成商的工作变得非常困难,特别是对于 UI 的微小更改(曾经做过这样的事情:)
没有义务在 MVVM Light 中使用 ViewModelLocator,它只是一种运行良好的方式,并且非常容易理解(对于不熟悉 WPF/SL 微妙之处的阅读代码的人来说)。最后,这在很大程度上是一个偏好问题,但最近 ViewModelLocator 模式似乎越来越受欢迎(例如,请参阅这篇文章,其中通用 ViewModelLocator 与 MEF 一起使用)。
http://www .johnpapa.net/simple-viewmodel-locator-for-mvvm-the-病人-have-left-the-asylum/
最后,让我补充一点,我对 ViewModelLocator 当前的实现不是很满意MVVM Light,我想在下一个版本中提出一个更通用的解决方案。
The method you use (with implicitly typed DataTemplates) works OK in WPF, but unfortunately it does not work in Silverlight. This is one of the reason why I prefer to use a more explicit method which works in both worlds.
Also, implicitly typed DataTemplates can be a bit confusing, because it is not always quite clear where the template comes from. That can render the work of the integrator very difficult at times, especially for small changes to the UI (been there, done that :)
There is no obligation to use the ViewModelLocator in MVVM Light, it is just a way that works well and is quite easy to understand (for people reading the code who are not familiar with the subtleties of WPF/SL). In the end, it is very much a matter of preference, but lately the ViewModelLocator pattern seems to gain in popularity (see for example this post where a generic ViewModelLocator is used together with MEF).
http://www.johnpapa.net/simple-viewmodel-locator-for-mvvm-the-patients-have-left-the-asylum/
Finally, let me add that I am not very satisfied with the current implementation of the ViewModelLocator in MVVM Light, and I want to propose a much more generic solution in the next version.