ViewModel 中的 WPF 相关属性是否违反了 MVVM 最佳实践? [英] Are WPF related properties inside a ViewModel a violation of MVVM best practices?

查看:53
本文介绍了ViewModel 中的 WPF 相关属性是否违反了 MVVM 最佳实践?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里有一个例子来详细说明:

Here is an example case to elaborate:

我正在使用视图中的 ItemsControl 动态创建一个简单的条形图,并将这些项绑定到 BarGraphViewModel 中的 BarViewModel 集合(每个包含一个值的百分比).每个条应该有不同的颜色.颜色应该从一个集合中选择,例如{Color1, Color2, ..}

I am dynamically creating a simple Bar Graph using an ItemsControl in my View and binding the items to a collection of BarViewModels (each containing percentage a value) in my BarGraphViewModel. Each bar should have a different color. The colors should be chosen from a collection e.g. {Color1, Color2, ..}

集合本身是恒定的,但柱的数量取决于具体情况.

The collection itself is constant but the number of bars will depend on the circumstances.

一个简单的解决方案是像这样创建一个简单的 BarViewModel:

A simple solution would be to create a simple BarViewModel like so:

public class BarViewModel
{
    public int Percentage { get; set; }

    public SolidColorBrush Stroke { get; private set; }

    public BarGraphViewModel(SolidColorBrush stroke)
    {
        Stroke = stroke;
    }
}

(为简洁起见,我省略了属性更改和验证实现)

(I left out property changed and validation implementation for brevity)

现在我可以从 BarGraphViewModel 为每个百分比创建一个 BarViewModels,并传入从我的 Color 集合创建的适当的 ColorBrush.

Now I could just create a BarViewModels from my BarGraphViewModel for each percentage and pass in the appropriate ColorBrush created from my Color collection.

然后在 Xaml 中,我将创建一个简单的 ItemsTemplate 来绑定到这些属性.

Then in Xaml I would create a simple ItemsTemplate that will bind to these properties.

现在,因为它包含一个 SolidColorBrush 类型的属性,所以我的 ViewModel 依赖于 Presentation 框架,如果我想在另一个环境中使用它,就必须对其进行更改.

Only now, since it contains a property of type SolidColorBrush, my ViewModel depends on the Presentation framework and should I want to use it in another environment it will have to be changed.

这是否会破坏 MVVM 的最佳实践,或者是否可以接受(您必须在某处划清界限,否则事情会变得太复杂)

Does this therefore break MVVM best practices, or is it acceptable (you gotta draw the line somewhere or things get too complicated)

我只是想看看其他人对此有何看法,以及是否有其他解决方案可以让 ViewModel 完全不了解表示层,而不会变得太复杂.我可以想象 ValueConverters 会有所帮助吗?

I just wanted to see what other people think about this and if there are other solutions that keep the ViewModel totally ignorant of the Presentation Layer without getting too complicated. I could imagine that ValueConverters could help?

推荐答案

在 Martin Fowler 描述的原始演示模型模式中,视图询问"视图模型如何显示自身.这似乎有利于将颜色和大小属性放在您的视图模型上,而不是在您的视图中触发.但是,此模式在 WPF 中的应用有些不同.在 WPF 中,您通常通过在视图中使用样式和数据模板来定义视图的方式.直接从视图模型返回特定颜色将与这种方法相反.所以简短的回答是:不,不要把颜色属性放在你的视图模型上.

In the original Presentation Model pattern described by Martin Fowler, the view "asks" the view-model how to display itself. This seems to favor putting color and size properties on your view-model rather than triggers in your view. The application of this pattern within WPF, however, is somewhat different. In WPF, you typically define how the view looks by using Styles and DataTemplates in the view. Returning specific colors directly from the view-model would be contrary to this approach. So the short answer is: no, don't put color properties on your view-model.

同样在原始演示模型模式中,视图模型是视图的抽象.因此,与其返回确切的颜色,不如返回一个键",然后视图可以使用它来查找实际颜色.例如,不是 PersonViewModel.FaceColor 返回 Red,而是 PersonViewModel.Mood 返回 Angry.然后,视图可以使用 Style 或 DataTemplate 触发器将其转换为实际的红色.

Also within the original Presentation Model pattern, the view-model is an abstraction of the view. So instead of it returning the exact color, it would be preferable to return a "key" that the view can then use to look up the actual color. For example, instead of PersonViewModel.FaceColor returning Red, you'd have PersonViewModel.Mood returning Angry. The view could then use a Style or DataTemplate trigger that translates this to the actual Red color.

这就是我的答案,我一直在坚持,但考虑另一个方向的论点也很有趣.一方面,将颜色属性放在您的视图模型上仍然是可单元测试的,这似乎已成为视图模型中正常情况的主要标准.

So that's my answer and I'm sticking by it, but it's also interesting to consider arguments in the other direction. For one, putting color properties on your view-model is still unit-testable, which seems has become the primary critera for what's okay in the view-model.

对视图技术保持不可知论"在任何一个方向都不是一个重要因素.保持视图模型与其他视图技术的二进制兼容性的目标只有在 XAML 家族中才是现实的.将所有视图模型移动到他们自己的项目中,该项目缺乏对 WPF 的直接依赖,这是一个不错的主意.但是您必须排除使用 ICommand 的任何内容,或者对 WindowsBase.dll 引用进行例外处理.但实际上,它不会给你带来太多好处.我们非常依赖 Micrsoft 技术!如果您决定移植到另一个 GUI 框架,那么我猜您正在查看源代码转换.在我尝试之前,我正在等着看 Microsoft 是否会破产;) 如果您决定将它们放入视图模型中,则移植可能包括更改颜色类型.虽然不是反对视图模型颜色属性的理由,但也不是必要的理由.

Remaining "agnostic" to the view technology isn't a huge factor in either direction. The goal to maintain binary compatibility of view-models with other view technologies is realistic only within the XAML family. Moving all your view-models to their own project which lacks a direct dependency on WPF is a nice idea. But you would have to exclude anything that uses ICommand, or make an exception for a WindowsBase.dll reference. Practically speaking, though, it won't buy you much. We're pretty much glued to Micrsoft technologies! If you decide to port to another GUI framework, then my guess is you're looking at source code conversion. I'm waiting to see if Microsoft goes under before I try it ;) Porting could include changing your color types, if you decided to put them in your view-model. Although not a reason against color properties on your view-model, it's not necessary a reason for it either.

这篇关于ViewModel 中的 WPF 相关属性是否违反了 MVVM 最佳实践?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆