2010-01-15 28 views
24

這一直困擾着我,也許我失去了一些東西。爲什麼我不能在XAML中評論屬性?

以下對註釋屬性(期望>)拋出錯誤,但我不應該能夠做到這樣的事情?

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

WPF:向前一步,兩步回來。它遍佈WPF,不是嗎。 – 2011-12-27 21:48:32

回答

20

儘管您不能使用基本的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

聖地獄!!!!!! – 2013-10-09 18:41:16

4

您不能在元素中使用類似的註釋。

這對所有XML都是正確的,而不僅僅是XAML。

看看XML Comments規範,它明確地禁止這種標記。

0

不,你不應該。 XML不能以這種方式工作 - 註釋節點不是屬性,所以它不能到屬性應該在的位置。

0

我在Laurent Bugnion's blog看到一個有趣的方法來評論屬性。

本質上,他定義了一個「忽略」命名空間,然後將「忽略」前綴添加到他想忽略的任何屬性。

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

簡短回答:因爲在<>之間不允許有< char(通過XML定義)。

下一個問題應該是「我怎樣才能註釋的XML/XAML屬性」

將溶液(例如,在MS共混物/ 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)" 
> 

的「令牌」 remTODODISABLEDNOTE只是我的建議和任何其他(有效的XML名稱)是可能的。

中的任何元素

實踐樣本:

的Unicode字符的下面的列表是有效的XML名稱:

ǀǁǂǃ

<TextBox 
    DISABLED:Background="#FF000000" NOTE:Background="temporary disabled" 
    Background="#FFFFFF"    TODO:Background="specify an other background" 
    TODO:TextBox="complete the textbox" 
> 

的Unicode字符的用法

<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..." 
    /> 
> 
相關問題