Xaml Fxcop Rules Using Xaml Toolkit
Xaml Toolkit ... ding ding ding... if you heard and tried it..woohoo!!. If not, please go and give it a spin : Xaml Toolkit
One of the nice things that you can do with it is write XAML FXCop rules. Now there has been a common ask of getting FXCop running on Xaml. Currently VS doesnt support this; but now you have the functionality at your tooltips. The toolkit has a Microsoft.Xaml.Tools.FxCop.dll which provides the base structure for your rules. So you could easily extend the inbuilt set of rules with that of your own.
So I tried a very simple rule to check if there are multiple access modifiers ie the presence of more than one _ in the content ...
<Button>_Hel_lo</Button> While this may be what the user wants, generally thats not the case. :) ... Note that this is just a simplistic scenario to show the usage. However, it could easily be extended to determine if there are duplicate access modifiers and this would be equivalent to the Globalization FxCop rule: AvoidDuplicateAccelerators.
namespace TestRule
{
public class AvoidMultipleAccelerators : BaseXamlRule
{
public AvoidMultipleAccelerators(): base("AvoidMultipleAccelerators", "TestRule.Rules", typeof(AvoidMultipleAccelerators).Assembly){}
public override void CheckXaml(XamlDomObject rootObjectNode, XamlSchemaContext schemaContext, string resourceName)
{
foreach (XamlDomObject obj2 in rootObjectNode.DescendantsAndSelf())
{
XamlDomMember nameMember = obj2.GetMemberNode("Content");
if (nameMember != null)
{
XamlDomValue nameValue = nameMember.Item as XamlDomValue;
if (nameValue != null)
{
if (nameValue.Value.ToString().Split('_').Length >2)
{
base.Problems.Add(base.CreateProblem(base.GetResolution(new object[] { nameValue.Value.ToString(), nameValue.StartLineNumber, nameValue.StartLinePosition }), nameMember.Member.Name, nameMember, resourceName));
}
}
}
}
}
}
}
The object array created in the last highlighted line is used for the message. The rules are exposed through a Rules.xml file which has the syntax like
<Rules FriendlyName="TestRule">
<Rule TypeName="AvoidMultipleAccelerators" Category="Microsoft.Xaml" CheckId="MSX1004">
<Name>Avoid Multiple Access modifiers.</Name>
<Description>Avoid Multiple Access modifier ( _ char).</Description>
<Url></Url>
<Resolution>Consider changing the value '{0}' in XAML. Line# {1} Position# {2} </Resolution>
<MessageLevel Certainty="95">CriticalError</MessageLevel>
<Email/>
<FixCategories>NonBreaking</FixCategories>
<Owner/>
</Rule>
</Rules>
And now that your are done with the rule, on running, it will look like
The code for this rule is attached . Note that you would need to download the Toolkit for the binaries in order to compile the project
Comments
Anonymous
August 01, 2010
This is Windows Client Developer roundup #35. The Windows Client Developer Roundup aggregates informationAnonymous
August 01, 2010
This is Windows Client Developer roundup #35. The Windows Client Developer Roundup aggregates informationAnonymous
August 01, 2010
This is Windows Client Developer roundup #35. The Windows Client Developer Roundup aggregates information