views:

246

answers:

2

How to enforce that developers writing XAML in Visual Studio should follow certain standards and validations need to be run and if invalid compile time errors are thrown.

For example, making sure that all the databinding expressions (some are real long) are written correctly as per 'a custom validation' I would like implement, during design time. Like,

<TextBox Text="{Binding Source={StaticResource CALCULATED}, Converter={StaticResource XPathConverter}, ConverterParameter=@FIRSTNAME_STRING, XPath=@FIRSTNAME}"/>

In the above sample if the binding expression in the Text property is not in that format, there should be a compilation error.

Is there a way to do this?

+2  A: 

There is no built-in way to do this. The best way you will be able to get this result is to run a custom tool on the input. This will require a lot of leg work on your part because it will involve parsing the file yourself but you should be able to get this scenario working.

Example site for creating a custom generator

http://www.drewnoakes.com/snippets/WritingACustomCodeGeneratorToolForVisualStudio/

JaredPar
+1 Thanks, but by writing custom tools for xaml files, I will be spitting out cs or other kind of file attached to it, which we want to avoid. How to direct errors as compile errors is the question. Wonder how...
Vin
I don't have a lot of experience with custom generators. I would try throwing and exception from the method and see if that produces an "error" in the task list.
JaredPar
Thanks Jared, then in that case custom generators may not be the answer to this design time problem, still thinking...
Vin
A: 

Sorry, the XAML language service can’t be extended this way either. The best way to do this today is to author a build task. I think you can hook it into the MarkupCompilePass1DependsOn target and it will be invoked automatically when the user saves or changes a XAML file. You still have to scan the file redundantly from us, but you don’t have to wait for an actual build to make this work. This is a direct quote from one of the Microsoft architects who currently is working on the WPF designers.