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

时间:2010-01-15 16:32:51

标签: xaml attributes comments

这困扰了我一段时间,也许我错过了什么。

以下内容会引发注释属性的错误(预期>),但我不应该这样做吗?

<Label x:Name="Gaga"
               FontSize="20"
               <!--
               Content="{Binding SomethingThatIsEmptyAtDesignTime"}
                -->
               Content="LookAtMe!"
               />

7 个答案:

答案 0 :(得分:24)

虽然您无法使用基本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!"
               />

blog post描述了如何执行此操作。

答案 1 :(得分:16)

简短回答:因为<<之间不允许使用>字符(通过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"
>

因此,如果您想要注释掉,只需在属性

中添加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)"
>

“令牌”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"
>

使用unicode字符:

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

ǀ ǁ ǂ ǃ

<TextBox
    ǃ:Background="temporary disabled"
    ǂ:Background="temporary disabled"
    ǁ:Background="temporary disabled"
>

用作文档(XML注释)

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

    <MyControl
        doc.summary="shows my control"
        doc.remarks="any remarks..."
    />
>

答案 2 :(得分:8)

因为XAML是基于XML的,XML doesn't allow comments inside other markup。不幸的是,我同意; XML评论还有很多不足之处。

答案 3 :(得分:4)

你不能在元素中使用这样的注释。

这适用于所有XML,而不仅仅是XAML。

看一下明确禁止这种标记的XML Comments规范。

答案 4 :(得分:1)

http://www.w3.org/TR/REC-xml/#sec-comments

评论可以存在于文档中的任何位置,而不是其他标记。

希望有所帮助!

答案 5 :(得分:0)

不,你不应该。 XML不是那样工作的 - 注释节点不是属性,因此它不能去属性所在的位置。

答案 6 :(得分:0)

我看到了一种在Laurent Bugnion's blog评论属性的有趣方法。

基本上,他定义了一个“ignore”命名空间,然后将“ignore”前缀添加到他想要忽略的任何属性中。

<ignore:ThisBlockIsIgnored Hello="World" Again="Blah">
<Label Content="No parse" />
</ignore:ThisBlockIsIgnored>