开发者

How to add comment for an attribute definition from within a tag?

Let's say we have the below code:

<Window 
    x:Class="Consus.Client.UI.Sandbox.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:ribbon="clr-namespace:Microsoft.Windows.Controls.Ribbon;assembly=RibbonControlsLibrary"

    x:Name="RibbonWindow"
    Width="800" Height="600"
    MinWidth="800" MinHeight="600"
    Title="MainWindow">

And we want to add a comment at the line MinWidth="800" MinHeight="600" to say "This is the minimum width/height of the application". I tried:

<Window 
        x:Class="Consus.Client.UI.Sandbox.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:ribbon="clr-namespace:Microsoft.Windows.Controls.Ribbon;assembly=RibbonControlsLibrary"

        x:Name="Rib开发者_开发问答bonWindow"
        Width="800" Height="600"
        MinWidth="800" MinHeight="600" <!--My comment goes here-->
        Title="MainWindow">

But this raised an error. So how can I do that?


It's not possible to add an comment to an attribute in this manner. XAML is an XML dialect and hence follows the syntatic rules of XML which dissallow this. XML comments can only appear outside of other markup elements (or more simply outside of other XML element tags).

The closest place it can be added is before or after the <Window> element.

<!-- Legal --> 
<Window
  ...

> <!-- Legal -->


  1. add namespace: xmlns:comment="my comment"

  2. Ignore it: mc:Ignorable="comment"

Tip: if you already ignoring some namespace like xmlns:d="http://schemas.microsoft.com/expression/blend/2008" just join these namespace using space: mc:Ignorable="a comment"

  1. Use it: Updated code:
<Window 
x:Class="Consus.Client.UI.Sandbox.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:ribbon="clr-namespace:Microsoft.Windows.Controls.Ribbon;assembly=RibbonControlsLibrary"
x:Name="RibbonWindow"
Width="800" Height="600"
MinWidth="800" comment:MinWidth="Some comment for MinWidth"
MinHeight="600" comment:MinWidth="Some comment for MinHeight"
Title="MainWindow">
...
</Window>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜