简体   繁体   English

如何将接口与代码分开?

[英]How can I divide interface from the code?

I know that I will be downvoted for this is kind of abstract question. 我知道我会因为这个抽象的问题而被否决。 I am aware that I'm missing the necessary specificity but this is a very important question to me. 我知道我缺少必要的特异性,但这对我来说是一个非常重要的问题。

I've been a WinForms C# user for ages. 我多年来一直是WinForms C#用户。 Now for work necessity I've been required to migrate to WPF. 现在出于工作需要,我已被要求迁移到WPF。 But what is puzzling me now is: am I using it properly? 但是,现在让我感到困惑的是:我使用得当吗? And specifically am I dividing xaml interface and code properly? 具体来说,我是否可以正确划分xaml接口和代码?

As a rule of thumb I put in the xaml interface all what I can and particularly all what's defined before hand. 根据经验,我会尽一切可能将xaml界面放入其中,尤其是事先定义好的所有内容。 On the other hand I'm letting in code behind all what's programmatically defined. 另一方面,我允许在所有以编程方式定义的内容后面添加代码。 Is that correct? 那是对的吗?

And then what are the advantages of putting all what I can in the xaml interface? 然后,将我所能拥有的全部放入xaml界面有什么优势?

Write just as little code in the code-behind files as you can. 您可以在代码隐藏文件中编写尽可能少的代码。 most of the time, you can get away with zero lines . 大多数时候,您可以摆脱零线

To react to UI-induced events, you should use the Commanding mechanism . 要对UI引发的事件做出反应,您应该使用Commanding机制 This will call a method, not in your Code-behind, but in a View-Model . 这将调用一个方法,而不是在您的Code-behind中,而是在View-Model中

Then you should google a bit on the MVVM topic. 然后,您应该在MVVM主题上浏览一下。

The reason why you want to separate your logic from your code-behind is so that code-behind is all about UI. 要将逻辑与后台代码分离的原因是,后台代码全部与UI有关。

if you have calculations to perform, you should not mix those concerns with the presentation layer. 如果要执行计算,则不应将这些问题与表示层混合使用。

For instance, you could want to have all your calculation in a portable library in the future so you could have a Windows phone version of your app, or a web-based service, or replace some heavy computation by a web-service. 例如,您将来可能希望将所有计算都存储在可移植的库中,这样您就可以拥有Windows Phone版本的应用程序或基于Web的服务,或者将某些繁重的计算替换为Web服务。

The yes philosophy here is to embrace SOLID principles, and WPF just promotes its use and allows to make cleaner separation for a better re-use and maintainability. 这里的“是”哲学是接受SOLID原则,WPF只是促进其使用,并允许进行更清洁的分离,以实现更好的重用性和可维护性。

There is nothing wrong with code-behind as long it doesn't contain program logic. 只要没有程序逻辑,代码隐藏就没有问题。 UI events and UI related code (for example: closing window or opening a new one) works perfectly in code-behind, and should be used there. UI事件和与UI相关的代码(例如:关闭窗口或打开一个新窗口)在后台代码中运行良好,应在此处使用。

The principle behind MVVM is to decouple UI specific routines from your business logic and functions. MVVM的原理是将特定于UI的例程与业务逻辑和功能分离。

Also a very important rule to keep in mind is that the viewmodels you create should be able to be reused in lets say asp.net site, windows mobile app or universal windows app. 还要记住的一个非常重要的规则是,您创建的视图模型应该能够在asp.net网站,Windows Mobile应用程序或通用Windows应用程序中重用。 So there's also no need to be UI aware. 因此,也无需了解UI。 A lot of times I see implementations where people glue the view (ui) and viewmodels together inside one project to overcome some limitations of viewmodels. 很多时候,我看到人们在一个项目中将视图(ui)和视图模型粘合在一起以克服视图模型的某些限制的实现。 This is however bad practice and defeats the main purpose to decouple your view from viewmodels and allow actually to reuse your viewmodel in other projects. 但是,这是不好的做法,并且无法实现将视图与视图模型分离并允许在其他项目中实际使用视图模型的主要目的。

To my students I advice therefore to start your viewmodel as portable class (this enforces you not to use ui elements inside your viewmodel) and depending of your view to extend them with platform specific functionality. 因此,对我的学生们来说,我建议您将ViewModel作为可移植类启动(这迫使您不要在ViewModel中使用ui元素),并根据您的视图使用特定于平台的功能来扩展它们。

Reusing your viewmodels is then a breeze. 重用您的视图模型很容易。

As glue between Viewmodels and views I recommend to use Prism Prism Project page and Nuget package 作为Viewmodel和视图之间的粘合,我建议使用Prism Prism Project页面Nuget包

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM