WPF MVVM code的背后WPF、MVVM、code

2023-09-02 20:47:26 作者:ー個亽遠行

我会尽量避免code后面的观点,在我的WPF MVVM的项目。

I try to avoid code behind in views, within my WPF MVVM project.

不过,我有一些事情是非常具体的看法。例如,当一个控制获得焦点我想要全文加以强调(即使用户点击到文本框中)。

However I have some things that are very specific to the view. For example when a control gets focus I want the full text to be highlighted (even if a user clicks into the text box).

在这里,我有选择的来处理这个视图模式(这则需要了解的观点,这是我想避免)。

Here I have a choice to handle this in the view model (which would then need to know about the view, which I want to avoid).

我也有一些其他的code一样,做事的用户界面,当用户presses上下左右在键盘上(他们只更改视图,而不是模型或视图模型)又一次我想最好的地方,这些是在考虑到后面的code。

I also have some other code like that does things to the UI when the user presses up down left or right on the keyboard (and they only make changes to the view, not the model or viewmodel) and again I'm thinking the best place for these is in the code behind of the view.

所以我要问,如果code仅影响视图(如光标移动的东西,例如,在文本框中等等,而不是模型或视图模型选择所有文字,是不是好放它在code的后面,而不是其他地方。

So I'm asking if the code only affects the view (e.g. things like cursor movement, selecting all text in a text box etc..., and not the model or view model, is it okay to put it in code behind, rather than elsewhere.

想知道什么是最好的做法在这里,如果其他人有更好的建议,其中把这个code。

Wondering what is best practise here, or if anyone else has a better suggestion where to put this code.

推荐答案

如果该行为是UI只有相关的,那么你不应该把它放在视图模型。你给的突出例子就是这种情况的一个很好的例子。话虽如此,我会建议你避免(例如)创建一个自定义的控制,突出的文本时它具有焦点重复你的code。这样,您就可以重新使用尽可能多的观点对照,你可以,你的意见可以免费的codebehind,如果您优化控制,在优化发生一刀切。

If the behavior is UI related only, then you should not put it in the ViewModel. The highlighting example you gave is a good example of such a case. Having said that, I would suggest you avoid repeating your code by (for example) creating a custom control that highlights the text when it has the focus. This way, you can reuse the control in as many views as you can, your views stay free of codebehind, and if you optimize your control, the optimizations happen across the board.

编辑:

鉴于拉维的回答,行为也是一种方式来介绍UI相关的逻辑,同时留下自由的codebehind查看。但是,如果您发现自己重复申报同一控件使用相同的行为,在我看来,这是更好地创建结合了动作的控制。

In light of Ravi's answer, Behaviors are also a way to introduce UI related logic while leaving the View free of codebehind. However, if you are finding yourself repeatedly declaring the same controls with the same behaviors, in my opinion it is better to create a control that incorporates the behavior.

这就是说,如果说UI逻辑将会出现一次在一个视图中,你可以考虑把它在codebehind。虽然这是相当罕见的提前知道,你不会在其他地方需要的逻辑。

That being said, if said UI logic is going to appear only once in one view, you may consider putting it in codebehind. Although it is quite rare to know in advance that you are not going to need that logic elsewhere.

编辑:

我觉得大力鼓励的@ ken2k的使用是指不要把它放进视图模型,这也是我提倡。 UI逻辑应在视图中实现的,如他说。现在,有这样做的几种方法。其中之一是直接在codebehind,这可能会导致重复code和维护问题对其进行编码。另外,如果你使用的单元测试,它可以让你在一个困难点。二是编码这样的逻辑成的行为,这是封装UI code的好办法。然后,您可以进行单元测试的行为,以确保它的工作原理确定。但是,你可以找到(像我一样,在很多项目中),您已经开始辣椒在XAML中的每一个文本框与行为的标签。如果开始发生,我会(并)创建一个HighlightedTextBox控制和使用,在我的XAML。总之,我的建议并不矛盾ken2k的,但是在解决您可能必须把逻辑为您浏览时,一些问题的方向指针。

I think @ken2k 's use of strong encouragement refers to not putting it in the ViewModel, which I also advocate. UI logic should be implemented in the View, as he says. Now, there are a few ways of doing that. One of these is coding it directly in your codebehind, which can lead to repetitious code and maintenance issues. Also, if you employ unit testing, it could put you in a difficult spot. The second is coding such logic into behaviors, which is a good way to encapsulate UI code. You can then unit test the behavior to make sure it works OK. However, you can find (as I did, in many projects) that you have started to pepper every TextBox in your XAML's with behavior tags. If that starts to happen, I would (and have) create a 'HighlightedTextBox' control and use that in my XAML. In summary, my suggestion does not contradict ken2k's, but is a pointer in the direction of resolving some issues you may have when placing logic for your View.

 
精彩推荐