WPF——在代码和XAML之间有什么区别?

时间:2022-10-10 23:24:08

I'm a long-time C#/.NET programmer but totally new to WPF and the System.Windows.Controls namespace and XAML. The more I learn about it the more I realize that you can do pretty much all of your GUI initialization and event handling glue in either XAML or in code (say C# code or VB.Net code).

我是一个长期的c# /。NET程序员,但对WPF和System.Windows来说是全新的。控件名称空间和XAML。我对它了解得越多,就越能意识到您几乎可以在XAML或代码(比如c#代码或VB)中进行所有GUI初始化和事件处理。Net代码)。

My question is to those who have been working on WPF for longer and ideally those who have shipped apps with it -- where did you find was the best place to 'draw the line' between XAML and code? Did you use XAML wherever you could? Only where interfacing with non-coding UI designers?

我的问题是,对于那些已经在WPF上工作了很长时间的人,理想的情况是,那些已经发布了应用程序的人——您在哪里找到了在XAML和代码之间“划清界线”的最佳地点?你在任何地方都使用XAML吗?只有在与非编码UI设计器交互的地方?

Any tips in this area would be extremely helpful to myself and other coders who are just getting into WPF programming and are kind of paralyzed by all the choices we can make!

这方面的任何技巧都对我和其他刚开始学习WPF编程的程序员非常有帮助,他们对我们所能做的所有选择都有点不知所措!

8 个解决方案

#1


19  

One thing that I would look at is the model-view-view model pattern. It is a very elegant pattern which naturally separates everything into nice buckets ... including your xaml.

我要研究的一件事是模型-视图-视图模型模式。这是一种非常优雅的模式,它将一切自然地分割成漂亮的桶……包括你的xaml。

For example, it helps you keep a clear boundary between the developer and the designer and even allows for test driven development.

例如,它可以帮助您在开发人员和设计人员之间保持清晰的界限,甚至允许进行测试驱动的开发。

There is a whole bunch of info out there on it, but I would start with John Gossman's blog posts:

上面有很多信息,但我想先从约翰·戈斯曼的博客开始:

Update: Just want to point people to another * post with lots of good info on M-V-VM in it.

更新:只是想让人们看到另一个*贴子,里面有很多关于M-V-VM的好信息。

#2


14  

One tip is to not declare event handlers in XAML. Instead, name your elements and attach events handlers in the code-behind. That helps keep a clean separation between the designer and developer.

一个技巧是不要在XAML中声明事件处理程序。相反,命名您的元素并在代码背后附加事件处理程序。这有助于在设计人员和开发人员之间保持清晰的分离。

#3


10  

As others have suggested, try following the Model-View-ViewModel pattern. However, it's OK to put stuff in the codebehind! The rule is that if it's "View" related, you put it in the Xaml or the codebehind (whichever is more convenient for you). If it's more business logic related to how the user interacts with the system, then it belongs in the ViewModel. If it's just business logic not concerned with interaction, it belongs in the Model.

正如其他人所建议的,尝试遵循模型-视图-视图模型模式。但是,把东西放在代码后面是可以的!规则是,如果它与“视图”相关,则将它放在Xaml或后面的代码中(哪个对您更方便)。如果它是与用户如何与系统交互有关的业务逻辑,那么它属于ViewModel。如果它只是与交互无关的业务逻辑,那么它属于模型。

Examples of each would be:

每一个例子将是:

  • Model: defines a property called ModifiedDate that stores the last time it was modified.

    Model:定义一个名为ModifiedDate的属性,该属性存储上一次修改的时间。

  • ViewModel: converts the ModifiedDate into an enumeration property called ModifiedAge, based on when it was modified: Yesterday, In the Last Week, In the Last Month, In the Last Year, etc.

    ViewModel:根据修改时间将修改日期转换为一个名为ModifiedAge的枚举属性:昨天、上周、上个月、去年等等。

  • View: converts the ModifiedAge property to a background color where more recently accessed data is highlighted bright yellow, and less recently accessed data is more of a beige-khaki-gray that your designer insists is called "Meadow Lark Lilly Flowerpot".

    视图:将ModifiedAge属性转换为背景色,其中最近访问的数据高亮显示为亮黄色,最近访问的数据更多地是一种“米色-khaki-gray”,您的设计人员坚持称之为“草甸云雀莉莉花盆”。

#4


6  

Another tip is to separate XAML into functional and aesthetic. Developers would typically work in the functional XAML whilst designers care primarily about the aesthetic. This keeps the functional XAML very easy to grok, which is important because developers often need to edit such XAML. Aesthetic XAML is typically edited by designers using tools, so its neatness and verbosity is less of an issue.

另一个技巧是将XAML分离成功能和美感。开发人员通常在功能性XAML中工作,而设计人员则主要关注美学。这使得XAML的功能很容易理解,这一点很重要,因为开发人员经常需要编辑XAML。美学XAML通常是由使用工具的设计师编辑的,所以它的整洁和冗长不是问题。

I did a bit of a blog post on this a while ago here.

我刚才在这里写了一篇博文。

#5


4  

Don't lose sight of the fact that XAML is code. It's declarative and all that, but it's still a programming language. In fact, it goes through a conversion to C# or Visual Basic before it's turned into IL for the .NET compiler to chew on.

不要忽略XAML是代码这一事实。它是声明性的,但它仍然是一种编程语言。事实上,在它变成IL之前,它会被转换成c#或者Visual Basic,让。net编译器去咀嚼。

I'll echo Scott Whitlock's comment: MVVM is a great way to separate concerns and focus on architectural details. It's really, really OK to put stuff in your code-behind, especially the stuff he describes. If you don't have a requirement to separate designer from developer, then adapt the MVVM pattern to your specific needs; don't try to force yourself to be pure or idealistic about it.

我将重复Scott Whitlock的评论:MVVM是分离关注点并关注架构细节的好方法。把东西放到代码后面真的很好,尤其是他描述的东西。如果您没有要求将设计人员与开发人员分开,那么请根据您的具体需求调整MVVM模式;不要试图强迫自己做一个纯粹的或者理想化的人。

It's also perfectly OK to put calls to your ViewModel's methods right in the View code behind, if you don't need the flexibility of commanding with ICommand classes. Or if you know the View you're making will always be bound to the ViewModel class you're making. Or you could take things a step further, define an interface for your ViewModel, and bind only to implementations of that interface. Then you can still swap out the ViewModel whenever you want.

如果不需要使用ICommand类的灵活性,也可以将对ViewModel方法的调用放在后面的视图代码中。或者,如果你知道你正在制作的视图将永远被绑定到你正在制作的ViewModel类。或者您可以更进一步,为您的ViewModel定义一个接口,并且只绑定到该接口的实现。然后您仍然可以随时交换ViewModel。

Stuff like that.

诸如此类。

#6


3  

When you follow a proper pattern like Mode-View-ViewModel you will get opportunity to do more on XAML side and less on code behind. Maximize the usage of RoutedEvents and Commands in WPF code.

当您遵循模式-视图-视图-视图模型这样的适当模式时,您将有机会在XAML方面做得更多,而在代码后面做得更少。最大限度地使用WPF代码中的路由和命令。

#7


2  

When building UserControls I try to Xamlize as much as possible.

在构建UserControls时,我尝试尽可能多地Xamlize。

One tip I found in the field is that creating ControlTemplate and DataTemplates by hand is really a pain in the ***... I always write those in XAML....

我在该领域中发现的一个技巧是,手工创建ControlTemplate和DataTemplates是非常痛苦的事情。我总是写在XAML ....

#8


1  

I would say use as much xaml possible, using Binding, commands, styles, templates etc. I had to support functionality of saving and loading templates using XAMLReader/XAMLWriter and it more easier for controls having more xaml.

我想说尽可能多地使用xaml,使用绑定、命令、样式、模板等等。我必须支持使用XAMLReader/XAMLWriter来保存和加载模板的功能,而且对于拥有更多xaml的控件来说,使用xaml更容易。

#1


19  

One thing that I would look at is the model-view-view model pattern. It is a very elegant pattern which naturally separates everything into nice buckets ... including your xaml.

我要研究的一件事是模型-视图-视图模型模式。这是一种非常优雅的模式,它将一切自然地分割成漂亮的桶……包括你的xaml。

For example, it helps you keep a clear boundary between the developer and the designer and even allows for test driven development.

例如,它可以帮助您在开发人员和设计人员之间保持清晰的界限,甚至允许进行测试驱动的开发。

There is a whole bunch of info out there on it, but I would start with John Gossman's blog posts:

上面有很多信息,但我想先从约翰·戈斯曼的博客开始:

Update: Just want to point people to another * post with lots of good info on M-V-VM in it.

更新:只是想让人们看到另一个*贴子,里面有很多关于M-V-VM的好信息。

#2


14  

One tip is to not declare event handlers in XAML. Instead, name your elements and attach events handlers in the code-behind. That helps keep a clean separation between the designer and developer.

一个技巧是不要在XAML中声明事件处理程序。相反,命名您的元素并在代码背后附加事件处理程序。这有助于在设计人员和开发人员之间保持清晰的分离。

#3


10  

As others have suggested, try following the Model-View-ViewModel pattern. However, it's OK to put stuff in the codebehind! The rule is that if it's "View" related, you put it in the Xaml or the codebehind (whichever is more convenient for you). If it's more business logic related to how the user interacts with the system, then it belongs in the ViewModel. If it's just business logic not concerned with interaction, it belongs in the Model.

正如其他人所建议的,尝试遵循模型-视图-视图模型模式。但是,把东西放在代码后面是可以的!规则是,如果它与“视图”相关,则将它放在Xaml或后面的代码中(哪个对您更方便)。如果它是与用户如何与系统交互有关的业务逻辑,那么它属于ViewModel。如果它只是与交互无关的业务逻辑,那么它属于模型。

Examples of each would be:

每一个例子将是:

  • Model: defines a property called ModifiedDate that stores the last time it was modified.

    Model:定义一个名为ModifiedDate的属性,该属性存储上一次修改的时间。

  • ViewModel: converts the ModifiedDate into an enumeration property called ModifiedAge, based on when it was modified: Yesterday, In the Last Week, In the Last Month, In the Last Year, etc.

    ViewModel:根据修改时间将修改日期转换为一个名为ModifiedAge的枚举属性:昨天、上周、上个月、去年等等。

  • View: converts the ModifiedAge property to a background color where more recently accessed data is highlighted bright yellow, and less recently accessed data is more of a beige-khaki-gray that your designer insists is called "Meadow Lark Lilly Flowerpot".

    视图:将ModifiedAge属性转换为背景色,其中最近访问的数据高亮显示为亮黄色,最近访问的数据更多地是一种“米色-khaki-gray”,您的设计人员坚持称之为“草甸云雀莉莉花盆”。

#4


6  

Another tip is to separate XAML into functional and aesthetic. Developers would typically work in the functional XAML whilst designers care primarily about the aesthetic. This keeps the functional XAML very easy to grok, which is important because developers often need to edit such XAML. Aesthetic XAML is typically edited by designers using tools, so its neatness and verbosity is less of an issue.

另一个技巧是将XAML分离成功能和美感。开发人员通常在功能性XAML中工作,而设计人员则主要关注美学。这使得XAML的功能很容易理解,这一点很重要,因为开发人员经常需要编辑XAML。美学XAML通常是由使用工具的设计师编辑的,所以它的整洁和冗长不是问题。

I did a bit of a blog post on this a while ago here.

我刚才在这里写了一篇博文。

#5


4  

Don't lose sight of the fact that XAML is code. It's declarative and all that, but it's still a programming language. In fact, it goes through a conversion to C# or Visual Basic before it's turned into IL for the .NET compiler to chew on.

不要忽略XAML是代码这一事实。它是声明性的,但它仍然是一种编程语言。事实上,在它变成IL之前,它会被转换成c#或者Visual Basic,让。net编译器去咀嚼。

I'll echo Scott Whitlock's comment: MVVM is a great way to separate concerns and focus on architectural details. It's really, really OK to put stuff in your code-behind, especially the stuff he describes. If you don't have a requirement to separate designer from developer, then adapt the MVVM pattern to your specific needs; don't try to force yourself to be pure or idealistic about it.

我将重复Scott Whitlock的评论:MVVM是分离关注点并关注架构细节的好方法。把东西放到代码后面真的很好,尤其是他描述的东西。如果您没有要求将设计人员与开发人员分开,那么请根据您的具体需求调整MVVM模式;不要试图强迫自己做一个纯粹的或者理想化的人。

It's also perfectly OK to put calls to your ViewModel's methods right in the View code behind, if you don't need the flexibility of commanding with ICommand classes. Or if you know the View you're making will always be bound to the ViewModel class you're making. Or you could take things a step further, define an interface for your ViewModel, and bind only to implementations of that interface. Then you can still swap out the ViewModel whenever you want.

如果不需要使用ICommand类的灵活性,也可以将对ViewModel方法的调用放在后面的视图代码中。或者,如果你知道你正在制作的视图将永远被绑定到你正在制作的ViewModel类。或者您可以更进一步,为您的ViewModel定义一个接口,并且只绑定到该接口的实现。然后您仍然可以随时交换ViewModel。

Stuff like that.

诸如此类。

#6


3  

When you follow a proper pattern like Mode-View-ViewModel you will get opportunity to do more on XAML side and less on code behind. Maximize the usage of RoutedEvents and Commands in WPF code.

当您遵循模式-视图-视图-视图模型这样的适当模式时,您将有机会在XAML方面做得更多,而在代码后面做得更少。最大限度地使用WPF代码中的路由和命令。

#7


2  

When building UserControls I try to Xamlize as much as possible.

在构建UserControls时,我尝试尽可能多地Xamlize。

One tip I found in the field is that creating ControlTemplate and DataTemplates by hand is really a pain in the ***... I always write those in XAML....

我在该领域中发现的一个技巧是,手工创建ControlTemplate和DataTemplates是非常痛苦的事情。我总是写在XAML ....

#8


1  

I would say use as much xaml possible, using Binding, commands, styles, templates etc. I had to support functionality of saving and loading templates using XAMLReader/XAMLWriter and it more easier for controls having more xaml.

我想说尽可能多地使用xaml,使用绑定、命令、样式、模板等等。我必须支持使用XAMLReader/XAMLWriter来保存和加载模板的功能,而且对于拥有更多xaml的控件来说,使用xaml更容易。