【问题标题】:Why can't I comment attributes in XAML?为什么我不能在 XAML 中评论属性?
【发布时间】:2011-01-05 14:23:42
【问题描述】:

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

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

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

【问题讨论】:

  • WPF:前进一步,后退两步。一切都在 WPF 上,不是吗。

标签: xaml attributes comments


【解决方案1】:

虽然您无法使用基本 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 描述了如何做到这一点。

【讨论】:

【解决方案2】:

简短回答:因为&lt;&gt; 之间不允许使用&lt; 字符(根据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"
>

从 VS2015+ 开始,仅支持特定属性,但请阅读下文...

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

<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 ="ignorable"
    xmlns:TODO ="ignorable"
    xmlns:DISABLED ="ignorable"
    xmlns:NOTE ="ignorable"
    mc:Ignorable="d rem TODO DISABLED NOTE"         
    rem:Background="this is also ignored (commented out)"
    TODO:Background=" (commented as TODO)"
    DISABLED:Background="this is also ignored (commented as DISABLED)"
>

“令牌”remTODODISABLEDNOTE 只是我的建议,任何其他(有效的 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 cmets)

<RootElement
    ...
    xmlns:doc="ignorable"
    mc:Ignorable="... doc ..." 
    
    <MyControl
        doc.summary="shows my control"
        doc.remarks="any remarks..."
    />
>

更新适用于 VS 2015 及更高版本

以上示例已更新

因为命名空间xmlns:d="http://schemas.microsoft.com/expression/blend/2008"现在已经被VS验证了,所以你只能对定义的成员使用它。 但是您可以指定自己的命名空间,例如xmlns:rem="ignorable" 和使用前缀 rem 或任何其他用户定义的前缀来制作没有 VS 设计器错误的 cmets。
使用 VS 2019 测试

【讨论】:

  • 此解决方案使 Visual Studio 2015 中的设计器出错。将 comment:NotAnAttribute="should be a comment" 放入 UserControl 会阻止设计器呈现该 UserControl。运行时行为很好。
【解决方案3】:

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

【讨论】:

    【解决方案4】:

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

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

    查看XML Comments 规范,该规范明确禁止此类标记。

    【讨论】:

      【解决方案5】:

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

      注释可以存在于文档中的任何位置,在其他标记之外。

      希望有帮助!

      【讨论】:

        【解决方案6】:

        不,你不应该。 XML 不能那样工作 - 注释节点不是属性,所以它不能去属性应该在的地方。

        【讨论】:

          【解决方案7】:

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

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

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

          【讨论】:

            猜你喜欢
            • 2023-03-05
            • 2021-04-21
            • 1970-01-01
            • 1970-01-01
            • 2013-05-19
            • 2021-05-24
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多