为什么我不能在XAML中评论属性?

Eri*_*ber 28 xaml attributes comments

这困扰了我一段时间,也许我错过了一些东西.

以下引用了注释属性(expected>)的错误,但是我不能做这样的事情吗?

<Label x:Name="Gaga"
               FontSize="20"
               <!--
               Content="{Binding SomethingThatIsEmptyAtDesignTime"}
                -->
               Content="LookAtMe!"
               />
Run Code Online (Sandbox Code Playgroud)

Eri*_*ber 26

虽然您无法使用基本XAML标记进行注释,但您可以通过导入Open XML标记命名空间来实现所需的结果.

xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:ignore="http://www.galasoft.ch/ignore"
mc:Ignorable="ignore"

<Label x:Name="Gaga"
       FontSize="20"
       ignore:Content="{Binding SomethingThatIsEmptyAtDesignTime"}
       Content="LookAtMe!"
/>
Run Code Online (Sandbox Code Playgroud)

这篇博文描述了如何做到这一点.

  • 博客文章的链接已断开。[博客帖子现在在这里](https://galasoft.ch/posts/2010/02/quick-tip-commenting-out-properties-in-xaml)。 (2认同)

kux*_*kux 20

答案很简单:因为一个<字符是不允许之间<>(通过XML定义).

下一个问题应该是"如何注释掉XML/XAML属性"

解决方案(例如,在MS Blend/Visual Studio中)是一个mc:Ignorable属性.

<RootElement
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    mc:Ignorable="d" 
    d:DataContext="this is an attribute for design time only"
>
Run Code Online (Sandbox Code Playgroud)

因此,如果要注释掉,只需d:在属性中添加前缀即可

为了更有用,你可以拥有更多作为一个可忽略的前缀:

<RootElement
    xmlns   ="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x ="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:d ="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:rem ="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:TODO ="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:DISABLED ="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:NOTE ="http://schemas.microsoft.com/expression/blend/2008"
    mc:Ignorable="d rem TODO DISABLED NOTE" 
    d:Foo="this is ignored (design time only attribute)"
    rem:Background="this is also ignored (commented out)"
    TODO:Background=" (commented as TODO)"
    DISABLED:Background="this is also ignored (commented as DISABLED)"
>
Run Code Online (Sandbox Code Playgroud)

"令牌" rem TODO DISABLED NOTE只是我的建议,任何其他(有效的xml名称)都是可能的.

任何元素的实际样本:

<TextBox
    DISABLED:Background="#FF000000"  NOTE:Background="temporary disabled"
    Background="#FFFFFF"             TODO:Background="specify an other background"
    TODO:TextBox="complete the textbox"
>
Run Code Online (Sandbox Code Playgroud)

unicode字符的用法:

以下unicode字符列表对xml名称有效:

? ? ? ? ? ? ?

<TextBox
    ?:Background="temporary disabled"
    ?:Background="temporary disabled"
    ?:Background="temporary disabled"
>
Run Code Online (Sandbox Code Playgroud)

用作文档(XML注释)

<RootElement
    ...
    xmlns:doc="http://schemas.microsoft.com/expression/blend/2008"
    mc:Ignorable="... doc ..." 

    <MyControl
        doc.summary="shows my control"
        doc.remarks="any remarks..."
    />
>
Run Code Online (Sandbox Code Playgroud)


Jos*_*osh 8

因为XAML是基于XML的,并且XML不允许在其他标记内添加注释.不幸的是,我同意; XML评论还有很多不足之处.