Skip to content

What tools generate .g.cs from XAML? Can it be done at runtime?

May 5, 2006

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?


The answer:

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)


From → XAML (non-UI)

  1. Unknown permalink

    Three coding styles and one issue 🙂 I need to support run-time (byenduser) layout modification (move buttons, change pictures etc), and choosesecond way of using Xaml – xamlreader.Now I get another trouble – NavigationWindowdoesnt want to Navigate to custom (loaded) page instance(throw InvalidOperationException), but if I create a compiledpage(from add new item->WinFX Page) it works perfect. Does it means that if Iload page content dynamically, I can\’t use navigation?

  2. Unknown permalink

    Hello Rob,
    is there a way to let IIS compile the entire application? What i mean can i use a Xaml-app like an ASP app?
    I want only the XAML on the server and the server compiles the new version for me.

  3. Rob permalink

    From IIS you have a few options.
    1) send XAML down to IE – custom logic is not very easy to do today.
    2) send XAML down to an installed application (or xbap) – the application can provide some custom logic.
    On the server, you could choose to build an application via msbuild…however this is not something you\’d want to do with every request from a client.
    Perhaps you could tell me what functionality you are trying to build if those answers don\’t answer it all for you…

  4. Rob permalink

    Navigation works fine with compiled or uncompiled xaml.
    My guess is that you are putting something in the xaml that requires compilation today — like an event handler.
    You may want to follow up in detail on the WPF forums…see the link to the left..

  5. Unknown permalink

    Hello Rob,
    hmm.. complicated to describe (my english is not good).
    Let\’s try. For example i have a xaml page with inputfields on it and after a while i want to put a new inputfield on it but for this i dont want to compile the entire app. This is in ASP.NET possible, there is the IIS which knows of the new file and then it compiles the new page with codebehind.

  6. Unknown permalink

    If you can, please give some links to code examples, how to compile xaml file at runtime. I have some troubles with runtime loading of objects from xaml using XamlReader.Load. All is fine until there are some code behind or event handlers. XamlReader throw an exception "Must compile XAML file that specifies events.".

  7. Unknown permalink

    lotro gold lord of the rings gold lord of the rings online gold lotro gold lord of the rings gold lord of the rings
    online gold Warhammer gold
    Warhammer online gold Warhammer money War gold War money Tabula Rasa Credit lotro gold lord of the rings gold lord of the rings online gold lord of the
    rings online gold lord of the rings
    gold Tabula Rasa Credit World of Warcraft gold PotBS Doubloon Pirates of the
    Burning Sea Doubloon PotBS Gold Pirates of the Burning Sea
    Gold lotro gold lord of the rings gold lord of the rings online gold Tabula Rasa Credit Warhammer gold Warhammer online gold PotBS Doubloon PotBS gold Pirates of the Burning Sea
    gold Pirates of the
    Burning Sea Doubloon

  8. Unknown permalink

    Yghfrlm xmgfh7 wow gold wow gold wow gold 股票软件 股票软件 黑马股推荐 黑马股推荐 大智慧 大智慧 人力资源培训 人力资源培训 胶体磨 胶体磨 上海水泵 上海水泵 炒股软件 炒股软件 黑马股 黑马股 中高年 転職 中高年 転職 出会い系 出会い系 毛穴 黒ずみ 毛穴 黒ずみ wow gold wow gold wow gold wow gold wow gold wow gold wow gold wow gold wow gold wow gold wow gold wow gold wow gold wow gold 賃貸 賃貸 賃貸 wow gold wow gold wow gold wow gold wow gold wow gold wow gold

  9. Unknown permalink

    アーネスト  設計事務所 行政書士  アクサ アクサダイレクト     三井ダイレクト 有料老人ホーム  現金化   クレジットカード 現金化    ドロップシッピング  医院開業   ショッピング枠 現金化 為替     看護  ウェディング  結婚式   ウェディング 結婚式 沖縄旅行 クレジットカード 現金化  外国為替  引越  副業  賃貸  不動産 競馬予想 出会い系

  10. Unknown permalink

    Amberdigital Branch,Southern Stars Enterprises Co is specializing in the development and manufacturing of advertising displays, advertising player and LCD displays. Established in 1996, we have explored and developed the international market with professionalism. We have built a widespread marketing network, and set up a capable management team dedicated to provide beyond-expectation services to our customers.

    amberdigital Contact Us
    Southern Stars Enterprises Co (Hong Kong Office)
    Add:3 Fl, No.2, Lane 2, Kam Tsin Tsuen, Sheung Shui, Hong Kong
    Tel:+852 2681 4099
    Fax:+852 2681 4586
    Southern Stars Enterprises Co (Shenzhen Office)
    Add:DE, 16/F, Building 2, Nanguo Tower, Sungang Road, Shenzhen, China
    Tel:+86 755 2592 9100
    Fax:+86 755 2592 7171[aibhhhhccecggh]

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: