XAML 或 C# 代码隐藏
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1002604/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
XAML or C# code-behind
提问by user101375
I don't like to use XAML. I prefer to code everything in C#, but I think that I am doing things wrong.
我不喜欢使用 XAML。我更喜欢用 C# 编写所有代码,但我认为我做错了。
In which cases it is better to use XAML and when do you use C#? What is your experience?
在哪些情况下最好使用 XAML,什么时候使用 C#?你的经验是什么?
采纳答案by Will Eddins
Creating an entire window in C# can be a mess of code. The best thing about WPF is that XAML allows you to separate your design from your logic, making for much easier-to-read code.
在 C# 中创建整个窗口可能是一堆代码。WPF 的最佳之处在于 XAML 允许您将设计与逻辑分开,从而使代码更易于阅读。
I'll use C# when I need to create dynamic controls, but I tend to keep my general design, static storyboards, styles, datatemplates, etc. in XAML.
当我需要创建动态控件时,我将使用 C#,但我倾向于将我的一般设计、静态故事板、样式、数据模板等保留在 XAML 中。
回答by Rob Allen
I originally came from the Web side of development and am learning WPF/Silverlight at the moment. For me, the XAML model makes much more sense to me than WinForms ever did. I treat the XAML as if it were HTML and the .cs files just like a code-behind.
我最初来自开发的 Web 方面,目前正在学习 WPF/Silverlight。对我来说,XAML 模型对我来说比 WinForms 更有意义。我将 XAML 视为 HTML,而 .cs 文件就像代码隐藏一样。
回答by John Fisher
My experience is that some things are far quicker to do in C#, while most are quicker to do in XAML. When it takes 5 lines of C# code to do what a single line of XAML code can do, it's pretty easy for me to choose which is better.
我的经验是,有些事情在 C# 中要快得多,而大多数事情在 XAML 中要快得多。当需要 5 行 C# 代码来完成一行 XAML 代码可以完成的事情时,我很容易选择哪个更好。
回答by Eric Schneider
Some things are easier to maintain or debug in code.
有些东西在代码中更容易维护或调试。
回答by JP Alioto
Check out this videoon MVVM in WPF. If you want wrap your head around how to organize a WPF application vis-a-vis what goes in XAML, code behind and other abstractions, this is a great place to start.
在 WPF 中的 MVVM 上查看此视频。如果您想了解如何组织 WPF 应用程序与 XAML、代码隐藏和其他抽象中的内容相比,这是一个很好的起点。
回答by Brian Genisio
The urge to want to write your UIs in C# instead of XAML is really just a manifestation of how comfortable you are in XAML.
想要用 C# 而不是 XAML 编写 UI 的冲动实际上只是您对 XAML 的熟悉程度的一种表现。
For me, is is a personal goal to write as little code-behind as possible. Quite simply put, code behind is hard to unit-test, but can (and usually does) include logic that doesn't get tested. XAML is declarative (like HTML) and doesn't include any logic, so there is nothing to unit-test. I keep my view code in XAML and I keep my view logic in my ViewModel (MVVM) which is VERY easy to test.
对我来说,编写尽可能少的代码隐藏是我的个人目标。简而言之,后面的代码很难进行单元测试,但可以(并且通常会)包含未经过测试的逻辑。XAML 是声明性的(如 HTML)并且不包含任何逻辑,因此无需进行单元测试。我将视图代码保存在 XAML 中,并将视图逻辑保存在我的 ViewModel (MVVM) 中,这非常容易测试。
Once you become more comfortable with XAML, the more you will realize its benefits over view construction in procedural code... Using a pattern like MVVM, you take it a step further, and realize that code-behind is only useful in rare cases.
一旦您对 XAML 更加熟悉,您就会越多地意识到它在过程代码中构建视图的好处......使用像 MVVM 这样的模式,您可以更进一步,并意识到代码隐藏仅在极少数情况下有用。
回答by Kent Boogaart
It's not just about you, it's also about your team, some of whom may be designers.
这不仅与您有关,也与您的团队有关,其中一些人可能是设计师。
回答by DouglasH
Not to Mention that you will be able to do more in Xaml2009 that has to be done in code behind now.
更不用说您将能够在 Xaml2009 中做更多现在必须在后面的代码中完成的工作。
Unfortantly BAML won't fully support xaml 2009 in vs 2010 time frame so you can't compile xaml in 2010 time frame. And will have to wait for a later version of blend to do the full dev design loop. (later than 3)
不幸的是,BAML 不会在 2010 年的时间范围内完全支持 xaml 2009,因此您无法在 2010 年的时间范围内编译 xaml。并且必须等待更高版本的 blend 才能完成完整的开发设计循环。(晚于3)
Douglas
道格拉斯
回答by Fenton
The most important thing to bear in mind is that XAML is for presentation. All your presentation should be in the XAML. If you have logic, you keep that out of your XAML - in your C#.
要记住的最重要的事情是 XAML 用于演示。您的所有演示文稿都应该在 XAML 中。如果您有逻辑,则将其保留在 XAML 之外 - 在 C# 中。
Imagine swapping out your XAML file with one that LOOKS completely different - but still uses the same data - that's where the division should be.
想象一下,用一个看起来完全不同的 XAML 文件换掉你的 XAML 文件——但仍然使用相同的数据——这就是划分的地方。
回答by Daniel Earwicker
You can certainly go too far with XAML. Those who want their entire user interface (including logic, event handling relationships, etc) defined in XAML are probably missing the point.
您当然可以使用 XAML 走得太远。那些想要在 XAML 中定义整个用户界面(包括逻辑、事件处理关系等)的人可能没有抓住重点。
The aim of XAML is to provide a common format for determining how things should look. It should just be a description of how to lay things out, how to color and style them visually.
XAML 的目标是提供一种通用格式来确定事物的外观。它应该只是描述如何布置事物,如何在视觉上为它们着色和设计样式。
There is really very little point in trying to use it as a replacement for other aspects of C#, because C# has a permanent head-start in terms of programming features - reuse (defining types and functions), referring to variables, procedural programming, and even declarative or functional styles.
尝试使用它来替代 C# 的其他方面确实没有什么意义,因为 C# 在编程特性方面具有永久的领先优势——重用(定义类型和函数)、引用变量、过程编程和甚至声明式或函数式样式。
Personally I really like throwing together a UI with a Linq expression!
就我个人而言,我真的很喜欢将 UI 与 Linq 表达式组合在一起!
The ultimate absurdity was reached by a sample I saw where they used workflow actions as the children of a button to supply the Click
handler, so the whole program was in XAML. It sounds "cool", but the problem was that it was significantly more ugly and unreadable than the equivalent C# or VB.NET program, and so everything that is ready to use in C# has to be replaced by a more verbose, flaky equivalent. Nothing has actually been gained by this translation to an uglier syntax - it's the same program only more hideous. XML is a poor basis for the syntax of a general programming language. Start with the fact that the greater-than symbol has to be written as >
!
我看到的一个示例达到了最终的荒谬之处,其中他们使用工作流操作作为按钮的子项来提供Click
处理程序,因此整个程序都在 XAML 中。这听起来“很酷”,但问题是它比等效的 C# 或 VB.NET 程序明显更丑陋和不可读,因此准备在 C# 中使用的所有内容都必须替换为更冗长、易碎的等效程序。这种转换为更丑陋的语法实际上并没有得到任何好处——它是同一个程序,只是更可怕。XML 是通用编程语言语法的糟糕基础。首先,大于号必须写为>
!
In a parallel universe, Microsoft released C# 3.0 before they finished XAML. The XAML team adopted C# 3.0 object/list initializer syntax instead of XML as their syntax. And this whole debate never happened.
在平行世界中,微软在完成 XAML 之前发布了 C# 3.0。XAML 团队采用 C# 3.0 对象/列表初始值设定项语法而不是 XML 作为他们的语法。整个辩论从未发生过。