I have been tasked with utilizing stylecop on .xaml files. Does anyone have a good place to start looking for the best way to accomplish this task. I have drifted around the internet and have yet to find a good solution. Our development environment is VS 2010 WPF application. Thank you for your help.
-
1I am not sure if it's possible to use stylecop with xaml. However, I use Xaml Styler (http://xamlstyler.codeplex.com/) to format XAML codes properly. – Amit Nov 22 '11 at 07:04
3 Answers
StyleCop is a source analysis tool to increase the readability of it. Visual Studio itself would be a good place to start. When you start writing xaml using VS it automatically indents code.
Here is an example
<Window x:Class="WpfApplication3.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Button Content="Hi" />
</Grid>
</Window>
This is what is expected (I think)
<Window x:Class="WpfApplication3.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Button Content="Hi" />
</Grid>
</Window>

- 73
- 1
- 9
The Microsoft Xaml Toolkit has Fxcop integration you might find useful.
blog posting: http://blogs.msdn.com/b/wpf/archive/2010/07/28/microsoft-xaml-toolkit-ctp-july-2010-fxcop-integration.aspx
downloads: http://archive.msdn.microsoft.com/XAML

- 6,431
- 1
- 54
- 70
As per http://archive.msdn.microsoft.com/sourceanalysis, StyleCop only analyzes C# source code - XAML is a completely different language. If your boss or manager tasked you with using StyleCop on the .xaml
files - what they probably meant (and you should double check with them rather than take my word for it), is to analyse the associated xaml.cs
files. Every xaml file is a partial class - one part of the class is the XAML (which gets translated to an automatic xaml.designer.cs
file which you cannot and should not mess with) - and the other part of the class (often called the codebehind) is the .xaml.cs
. This document is one you can use StyleCop on, although some of it's rules may be confused by the fact that it's being run on only one half of a partial class.
That's the best you can hope to accomplish.

- 26,663
- 20
- 114
- 184
-
1Also you can try to analyze the "other part" of the xaml: indeed xaml is compiled into .cs, all files are put into the `/obj` folder with extension `.g.cs` – May 04 '12 at 05:50