Compiling Avalon XAML files
Posted: (EET/GMT+2)
Microsoft's Avalon graphics engine interests me, since I'm looking a way to get nice graphical UIs to my applications.
Since getting access to MSDN, I also started studying Avalon and the XAML files, among other things.
After you download and install the WinFX SDK and the Avalon technology preview, it is simple to develop Avalon applications using Visual Studio 2005. However, compiling the XAML files seems like magic, and I wanted to repeat this process outside VS2005.
This proved to be somewhat difficult. I found references to the XAML compiler (xamlc.exe), but I couldn't find this executable from my installation of the WinFX SDK and .NET 2.0 Beta 2.
So, I ended studying the new MSBuild engine and it's XML configuration files. Since MSBuild is also new to me, this took some time, but finally I found a way to do what I wanted.
To summarize, the MSBuild executable is part of the .NET Framework installation, and resides in a path like this:
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50215\MSBuild.exe
I won't be going into details about MSBuild here, but to operate and compile XAML files, MSBuild needs access to "target" files, and the WinFX SDK installation installs a target file like this:
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50215\Microsoft.WinFX.targets
This file defines the tasks needed to compile XAML files, among other things. Now, you can command the MSBuild engine using .PROJ files, which -- you guessed it -- are also XML files.
To build a XAML file named Test.xaml, I wrote a .PROJ file like this:
<Project
DefaultTargets="Build"
xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import
Project="$(MSBuildBinPath)\Microsoft.WinFX.targets" />
<PropertyGroup>
<IntermediateOutputPath>.</IntermediateOutputPath>
<AssemblyName>Test</AssemblyName>
<Language>C#</Language>
<OutputType>winexe</OutputType>
</PropertyGroup>
<ItemGroup>
<ApplicationDefinition Include="Test.xaml" />
<Page Include="Test.xaml" />
</ItemGroup>
</Project>
Next, I opened the SDK Command Window (from the Start menu group created by the .NET Framework 2.0 installation), and executed the following command:
msbuild /t:CodeGeneration
If you don't specify any filename, then MSBuild will look the current directory for a .PROJ file. As a result the task named "CodeGeneration" is the one that is able to generate a binary BAML file from a XAML file.
So in the end, this command took my Test.xaml file, and compiled it to these files:
Test.baml Test.g.cs Test.Main.g.cs
This way, you can build your XAML files manually, and also easily examine the generated intermediate files. You can see these using Visual Studio 2005 too, but it's not the same thing.