What tools generate .g.cs from XAML? Can it be done at runtime?
Got a question from a WPF user from within Microsoft yesterday which we’ll call Don (no, not this Don):
What tools are used to generate the .G.CS classes when a XAML project is created?
Also is there an API for this so I can do it at runtime?
In order to compile xaml files, we have declared a few MsBuild tasks in PresentationBuildTasks.dll.
Projects that contain <Page Include="Page1.xaml" /> and that import MicrosoftWinFX.targets will be markup compiled.
There are 2 "Targets" defined in that targets file that do the work for markup compilation.
MarkupCompilePass1 – creates a .g.cs file for each Page. If the xaml doesn’t refer to any classes defined in the assembly being created during the build, a baml file is created.
This baml file is a binary (pre-tokenized) version of the hierarchy of objects and property sets from the xaml file.
MarkupCompilePass2 – creates all baml files that weren’t able to be created in Pass1
The only APIs that WPF exposes to do this markup compilation are those msbuild based tasks in PresentationBuildTasks.dll.
MsBuild can be called at runtime (from full trust apps).
Here is some additional information that may help your investigation:
3 Coding Styles for Avalon Applications (this shows ways that markup and code can be mixed during compile and/or runtime)
.xaml, .baml, .g.cs details (discusses some details about baml, generated code)