WPF MVVM 代码背后 [英] WPF MVVM Code Behind

查看:19
本文介绍了WPF MVVM 代码背后的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尽量避免在我的 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).

我还有一些其他类似的代码,当用户在键盘上向左或向右按​​下时对 UI 执行操作(他们只更改视图,而不是模型或视图模型),我又在想这些最好的地方是在视图后面的代码中.

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.

所以我问的是代码是否只影响视图(例如光标移动、选择文本框中的所有文本等……而不是模型或视图模型,是否可以将其放入代码中?后面,而不是别处.

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.

想知道这里的最佳实践是什么,或者其他人是否有更好的建议将该代码放在哪里.

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

推荐答案

如果行为仅与 UI 相关,则不应将其放入 ViewModel.你给出的突出显示的例子就是这种情况的一个很好的例子.话虽如此,我建议您通过(例如)创建一个自定义控件来避免重复您的代码,该控件在具有焦点时突出显示文本.通过这种方式,您可以在尽可能多的视图中重用控件,您的视图不会有代码隐藏,如果您优化控件,优化就会全面进行.

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.

根据 Ravi 的回答,行为也是一种引入 UI 相关逻辑的方式,同时让视图没有代码隐藏.但是,如果您发现自己反复声明具有相同行为的相同控件,在我看来,最好创建一个包含该行为的控件.

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 逻辑仅在一个视图中出现一次,您可以考虑将其放入代码隐藏中.尽管很少提前知道您在其他地方不需要该逻辑.

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 的强烈鼓励是指不要把它放在ViewModel中,我也提倡.正如他所说,UI 逻辑应该在视图中实现.现在,有几种方法可以做到这一点.其中之一是直接在您的代码隐藏中对其进行编码,这可能会导致重复的代码和维护问题.此外,如果您使用单元测试,它可能会让您陷入困境.二是把这样的逻辑编码成行为,这是封装UI代码的好方法.然后,您可以对行为进行单元测试以确保其正常工作.但是,您会发现(正如我在许多项目中所做的那样)您已经开始在 XAML 中的每个 TextBox 中添加行为标签.如果这种情况开始发生,我会(并且已经)创建一个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.

这篇关于WPF MVVM 代码背后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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