Goran
Goran

Reputation: 6518

How to add xaml code in c# comment?

/// <summary>
/// 
/// </summary>
/// <remarks
/// 
/// </remarks>

How to add xaml code in above comment, avoiding code parser to create links that do not exist (CTRL + Click messages)?

Upvotes: 4

Views: 2210

Answers (3)

Paolo Moretti
Paolo Moretti

Reputation: 55956

You have two options:

  1. Standard XML escaping

    /// &lt;TextBlock Text=&quot;Hello World!&quot;/&gt;

  2. CDATA sections

    /// <![CDATA[<TextBlock Text="Hello World!"/>]]>

With the first option, Visual Studio Intellisense will properly show the unescaped XML sequence.

Upvotes: 6

Louis Kottmann
Louis Kottmann

Reputation: 16628

In XAML view, put some XAML you want to use as comment

Then surround it with

<!-- #region XAML Snippet -->
<StackPanel>
  <Button Content="Click me, I do nothing!" />
</StackPanel>
<!-- #endregion -->

Note: i put this in a MainWindow.xaml file, but it can be any XAML file.

Now in C#, to use this as a comment:

/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
/// <example>
/// <code source="MyProject.Namespace/MainWindow.xaml" region="XAML snippet" lang="XAML" title="A linked *.xaml file treated as XAML"/>
/// </example>

SandCastle understands this syntax, and will generate appropriate documentation (like in MSDN), however this won't show up on intellisense.

You could format a regular <summary> body with &gt; and &lt; instead of < > signs in order to mimic XAML syntax, but that's going to be a pain.

Upvotes: 6

ZombieSheep
ZombieSheep

Reputation: 29953

Use CDATA tags?

/// <summary>
/// <![CDATA[<StackPanel />]]>
/// </summary>

Note: this won't appear in Intellisense so if that is a requirement this method won't work for your scenario.

Why do you want to add XAML into a code file, though?

Upvotes: 2

Related Questions