Attention: We are retiring the ASP.NET Community Blogs. Learn more >

Manually Compile your XAML into BAML

For those of you who are hardcore console compilers, here's how you can manually compile your BAML partial classes, using the Windows Application Compiler:

The file, ac.exe, can be found in the %installdrive%:\WINDOWS\Microsoft.NET\Avalon directory. You can either apecify the project (*.lhproj) file or XAML. If you choose XAML, you can specify whether to compile to “full code” (instead of BAML) using the -code directive, all XAML files in the directory (the -dir ), or perhaps the strangest, “Create a combined source file only  (No IL)”, using the -so directive.

Update - from the comments, Rob Relyea writes that ac.exe is obselete, and XamlC.exe (available at the LHSDK prompt) should be used instead.  Thanks for the correction!

Update2 - Rob Relyea posts more on why to compile your XAML, when you don't have to compile, and what you need to have in order to do so here.

7 Comments

  • Don't use AC.exe! That is obsolete.

    As Chris has written, we did a major rearchitecture, AC.exe is part of the old architecture.



    What you should use is XamlC.exe (which is currently installed with the LHSDK). When you run the Longhorn SDK Command Prompt - it will be in the path.

  • So, the only way to properly compile is to use xamlc and not ac. Which also would mean I have to get the SDK and couldnt complile with just a LH install?

  • To use precompiled XAML, yes, however there is a runtime environment built into Longhorn that runs non-compiled XAML.

  • We want to get to a model that will allow building in LH without the sdk having to be installed.



    Here is what works now:

    1) Msbuild.exe is shipping with LH, becuase the .net redist is part of LH.



    Here are problems now:

    1) set lapi=c:\windows\microsoft.net\windows\v6.0.4030

    2) resgen.exe and al.exe need to be installed currently...i forget the path we are looking for it (perhaps %sdktoolpath% ????)

  • Ok, last night I put on the SDK (finally) and compiled a simple XAML with xamlc.exe. Awesome! I cant wait to do more!

  • Can someone tell me a good way to read BAML files? I tried the System.Windows.Serialization.BamlReader class, but it seemed not to work correctly - it gives some UIContext exception.



    Thank you in advance,

    Stan

  • The first step is to compile the XAML files into BAML using the xamlc.exe compiler.For example, if your project includes a file name Window1.xaml, the compiler will create a
    temporary file named Window1.baml and place it in the obj\Debug subfolder (in your project
    folder). At the same time, a partial class is created for your window, using the language of
    your choice. For example, if you’re using C#, the xamlc.exe compiler will create a file named
    Window1.g.cs in the obj\Debug folder. The g stands for generated.

    The partial class includes three things:
    • Fields for all the controls in your window.
    • Code that loads the BAML from the assembly, thereby creating the tree of objects. This
    happens when the constructor calls InitializeComponent().
    • Code that assigns the appropriate control object to each field and connects all the
    event handlers. This happens in a method named Connect(), which the BAML parser
    calls every time it finds a named object.
    The partial class does not include code to instantiate and initialize your controls because
    that task is performed by the WPF engine when the BAML is processed by the
    Application.LoadComponent() method.

    ■Note As part of the XAML compilation process, the XAML compiler needs to create a partial class. This is
    only possible if the language you’re using supports the .NET Code DOM model. C# and VB support Code
    DOM, but if you’re using a third-party language you’ll need to make sure this support exists before you can
    create compiled XAML applications.

    ...so read the filename.g.cs, it's how BAML is loaded.

Comments have been disabled for this content.