为什么老是背后的'干净'code和尽一切XAML?干净、code、XAML、尽一切

2023-09-04 00:52:55 作者:〆为你一世妖娆

什么是保持code后面的'干净'?

What is the benefit of keeping code behind 'clean'?

很多时候,我碰到过的帖子在这里对有人试图做等值的XAML,而不是code后面。是他们希望保持自己的身后'干净'code的唯一原因。纠正我,如果我错了,但事实并非如此说:  在XAML编译太 - 成BAML - 然后在运行时无论如何都要解析为code 。  在XAML中都可能有更多的运行时错误,因为他们不会被拾起由编译器在编译时 - 不正确的拼写 - 这些错误也难以调试  在目前已经落后code - 喜欢还是不喜欢

Many times I come across posts here about someone trying to do the equivalent in XAML instead of code behind. Their only reason being they want to keep their code behind 'clean'. Correct me if I am wrong, but is not the case that: XAML is compiled too - into BAML - then at runtime has to be parsed into code anyway. XAML can potentially have more runtime bugs as they will not be picked up by the compiler at compile time - from incorrect spellings - these bugs are also harder to debug. There already is code behind - like it or not

InitializeComponent();

已经被运行和.gics文件,它是包含了一堆code,虽然它可能是隐藏的。

has to be run and the .g.i.cs file it is in contains a bunch of code though it may be hidden.

它是纯粹的心理?我怀疑这是谁来自网络的背景和类似的标记,而不是code语言开发。

Is it purely psychological? I suspect it is developers who come from a web background and like markup as opposed to code.

编辑:我不建议code后面,而不是XAML - 同时使用 - 我preFER做我在XAML绑定过 - 我只是反对尽一切努力避免写code后面尤指一个WPF应用程序 - 它应该是一个融合两者,以获得最大的收益

I don't propose code behind instead of XAML - use both - I prefer to do my binding in XAML too - I am just against making every effort to avoid writing code behind esp in a WPF app - it should be a fusion of both to get the most out of it.

推荐答案

1。设计师的角度来看

用户界面往往是通过设计工具(如pression融合和朋友)设计建造。如果我有这样的worklow的,它只是简单地并不重要,如果你把一个显著量的UI相关的code。在codebehind工作。至少,这是我们所取得的经验。在code和行为/ functionalty它定义为inacessable设计师:

UIs are often built by designers using designer tools (expression blend and friends). If I have this kind of worklow, it simply just doesn't work if you put a significant amount of UI-related code in codebehind. At least that's the experience we made. The code and the behavior/functionalty it defines is inacessable to the designer:

在大多数情况下是code是仅 在运行时,而不是同时执行 设计。所以设计师并没有看到完整的故事而设计。 在设计师不是程序员和 只有有限的(如果有的话) porgramming技能,所以他们最 可能是无法精炼的 用户界面行为定义在那里。 For the most part that code is only executed at runtime and not while designing. So the designer doesn't see the full story while designing. Designers are not programmers and only have limited (if at all) porgramming skills so they most likely are incapable of refining the UI behavior defined there.

此外,我们已经取得了它变得非常难以找到一种方式来提供嘲笑designtime数据的经验( D:DesignInstance,D:DesignData,D:的DataContext )的设计师与是否有codebehind工作。

Additionally we have made the experience that it gets quite hard to find a way to provide mocked designtime data (d:DesignInstance, d:DesignData, d:DataContext) for the designer to work with if there is codebehind.

2。该开发者的角度

在codebehind UI相关的code(我在这里假设没有必要谈论把域逻辑在codebehind 的的几率)是code表示不能重复使用。这是永远绑定到一个特定的用户控件/窗/页code。如果我的例子,而不是会写一个自定义的附加属性或行为我得到resuablity再加上我做我们desginers高兴,因为他们可以使用它了。

UI-related code in codebehind (I am assuming here that it is unnecessary to talk about the odds of putting domain logic in codebehind) is code that is not reusable. It is code that is bound forever to that one specific UserControl/Window/Page. If I for example instead would write a custom attached property or a behavior I get resuablity plus I make our desginers happy because they can use it too.

所有code我把codebehind是code,是很难测试。当然,这主要是没有得到更容易只是把它在XAML或自定义附加属性。但是,这取决于什么样的功能,我将在codebehind有在那里我可以封装它可测试(可重复使用)班的情况。

All code I put in codebehind is code that is hard to test. Of course it mostly doesn't get easier just by putting it in XAML or in a custom attached property. But depending on what type of functionality I put in codebehind there are cases where I could have encapsulate it in testable (reusable) classes.

定义的外观和行为在XAML往往是少(相对于提问参数)容易出错比code。我不能让尽可能多的失误XAML,我可以在code。如果我做错了什么事有机会,我立刻看到它在设计/视觉工作室。当然,这些工具仍然可以在这里提高。逸岸,如果我另外使用 ReSharper的那些不正确的拼写miskates在XAML的提问提到的是几乎不可能使。我得到了code突出的时候了。我相信标准工具将挑选这件事。 XAML是preferred方式来定义用户界面在WPF和高得多的努力已经取得了微软,以确保其工作正常比使用code。逸岸我已经花了相当长的一段时间调试memoryleaks和运行时异常的code,它确实UI相关的东西,可能已经被转移到XAML很少或根本没有额外的努力。

Defining appearance and behavior in XAML tends to be less (as opposed to the questioners argument) error prone than in code. I just can't make as many mistakes in XAML as I can in code. If I did something wrong chances are that I see it right away in the designer/visual studio. Of course the tools still can improve here. Infact if I additionally use ReSharper those "incorrect spelling" miskates in XAML that the questioner mentions are almost impossible to make. I get that code highlighted right away. I am sure the standard tools will pick this up. XAML is the preferred way to define the UI in WPF and a much higher effort has been made by microsoft to assure that it works as expected than using code. Infact I have already spent quite some time debugging memoryleaks and runtime exceptions on code that did UI related stuff and could have been moved to XAML with little or no extra effort.

如果我放松了对codebehind禁欲存在较高的风险,我写clutterd和坏code。有时候,它只是诱人放一个快速的黑客在codebehind。我们从后果不止一次sufferd。

If I ease up on the codebehind abstinence there is a higher risk that I write clutterd and bad code. Sometimes it is just to tempting to put a quick hack in codebehind. We have sufferd from the consequences more than once.

使用codebehind很少真的有必要。一旦你习惯了视图模型驱动的用户界面有alomst从来没有一个justifyable必要性codebehind。它并不需要很多的努力,把它放在别的地方。何必呢?

Using codebehind is rarely really necessary. Once you get used to ViewModel driven UIs there is alomst never a justifyable necessity for codebehind. It doesn't take much effort to put it somewhere else. So why bother?