简体   繁体   English

在WPF中关闭标记的最佳实践

[英]Best practice for closing tags in WPF

I am currently reading the book Pro WPF in C# 2008 , and I'm a newbie to WPF. 我目前正在阅读C#2008中的Pro WPF一书,我是WPF的新手。

I basically have a questions regarding best practice, as I want to write code that is commonly accepted by other developers. 我基本上对最佳实践有疑问,因为我想编写其他开发人员普遍接受的代码。

I see throughout the book, that the author uses different ways of closing tags for different purposes. 我在整本书中都看到,作者使用不同的方法来关闭不同目的的标签。 I'll give an example: 我举个例子:

When creating a grid, and defining the rows and columns, he always write this code similar to this: 在创建网格并定义行和列时,他总是编写与此类似的代码:

<Grid.ColumnDefinitions>
  <ColumnDefinition />
  <ColumnDefinition />
</Grid.ColumnDefinitions>

So he closes the tag "inline" inside the tag since the tag does not have any content (I'm not sure if inline is the correct wording for this) 所以他关闭了标签内的标签“inline”,因为标签没有任何内容(我不确定内联是否是正确的措辞)

But most other places, he will put a closing tag, even if it's no content like this 但是大多数其他地方,即使不是这样的内容,他也会给出一个结束标记

<TextBlock Text="{Binding Path=CategoryName}"></TextBlock>

I also see, that if he defines any attributes in the ColumnDefinition case he will also add the closing tag like this 我也看到,如果他在ColumnDefinition案例中定义了任何属性,他也会像这样添加结束标记

<ColumnDefinition Width="Auto"></ColumnDefinition>

My question is simply. 我的问题很简单。 Are there any reason to write it like this? 有没有理由这样写? I think it looks more tidy to omit the closing tag is there is no contents in all cases. 我认为省略结束标签看起来更整洁,因为在所有情况下都没有内容。

So is this just a matter of personal preference, or are there any other reasons to switch between the two. 这只是个人偏好的问题,还是有其他理由在两者之间切换。

It's personal preference as the code is equivalent. 这是个人偏好,因为代码是等价的。

However, I can see that 但是,我可以看到

<TextBlock Text="{Binding Path=CategoryName}"></TextBlock>

will come about if the line is originally written as: 如果该行最初被写为:

<TextBlock Text="{Binding Path=CategoryName}">
    some other xaml
</TextBlock>

but then the "other xaml" is removed (for whatever reason) 但随后“其他xaml”被删除(无论出于何种原因)

What you should do is come up with a standard and stick to it (which ever way you decide to go). 你应该做的是提出一个标准并坚持下去(你决定去哪个方向)。

This is more of an XML thing than a WPF thing and mostly doesn't matter since they just parse the XML. 这更像是一个XML而不是WPF的东西,并且大多数并不重要,因为它们只是解析XML。 In general though I'd recommend always to use "Empty elements" (<tag/>) when you don't have any child elements since it both looks cleaner and prevents any accidental whitespace in the contents of the tag. 一般情况下,虽然我建议在没有任何子元素时始终使用“空元素”(<tag />),因为它看起来更干净并且可以防止标记内容中出现任何意外的空格。

He's probably just inconsistent in the book by accident 他可能只是偶然地在书中不一致

When adding a control that has no elements inside it, better to close it by self-closing tag “/>” instead of the hard closing tag . 添加其中没有元素的控件时,最好通过自动关闭标记“/>”而不是硬结束标记来关闭它。

eg. 例如。

<Label> 
    <Label.Content> 
        <TextBlock> 
            Test! 
        </TextBlock> 
    </Label.Content> 
</Label> 


<TextBlock Text={Binding Name}/>

In my opinion it is more of a personal taste. 在我看来,这更像是个人品味。 But having said that, there are some scenarios where you are forced to use notation like <keyword></keyword> . 但话说回来,在某些情况下,您不得不使用<keyword></keyword>等符号。 So, unless forced there are no reasons why you should write in one way or the other. 所以,除非被迫,否则你没有理由以某种方式写作。 I am adding this question to my favorite list to see if there is any special guideline. 我将此问题添加到我最喜欢的列表中,看看是否有任何特殊指南。

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

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