pencilCake
pencilCake

Reputation: 53263

When I build a Silverlight project, what happens to XAML files?

Can you please explain the life-cycle of an XAML file in terms of compiling?

When I build a Silverlight project what happens to a XAML file in the build process?

Upvotes: 4

Views: 693

Answers (2)

iCollect.it Ltd
iCollect.it Ltd

Reputation: 93601

The answer about the intermediate .g.cs file by Jon Skeet (now deleted, but that part quoted below for context) was partly correct but did not answer your actual question in full:

JS: An early part of the build process creates a Foo.i.g.cs file in the obj directory containing a partial class, and that gets compiled in the normal way along with your own .cs files.

The .g.cs files contain pieces missing from the code-behind required to connect named elements to class members during InitialiseComponent(). e.g. this is from a basic MainPage.g.cs:

public void InitializeComponent() {
    ...
    System.Windows.Application.LoadComponent(this, new System.Uri("/SilverlightApp1;component/MainPage.xaml", System.UriKind.Relative));
    this.LayoutRoot = ((System.Windows.Controls.Grid)(this.FindName("LayoutRoot")));
}

This is bit like the designer files generated for WinForms controls/dialogs, but happens at compile time instead of design time.

  • The compiler parses to validate the XAML and to determine what named elements need to be generated (into the partial class).
  • The XAML file itself gets stored as a resource in the DLL during the build (in the example above it is stored as "/SilverlightApp1;component/MainPage.xaml")
  • Note: For WPF only, the embedded XAML file is actually converted to a more memory-efficient binary version referred to as a BAML file.

Answering your comment to Jon Skeet (now deleted), you are partially correct in asking:

So parsing the whole XAML document -all the elements, attributes etc.- is not part of the WPF or SL builds? Am I correct?

Aside from the parsing mentioned above, for validation and named elements, the rest of the parsing (of the element tree and templates etc) is actually done at runtime when LoadComponent() effectively deserializes the XAML and creates a visual tree of elements you authored.

Upvotes: 3

JaggenSWE
JaggenSWE

Reputation: 2106

When you build a Silverlight project a .xap file is created, this is basically just a .zip-file with another extension, containing an AppManifest.XAML and your project in a DLL-file (together with DLL:s for other dependencies).

If you run the DLL through dotPeek or Reflector you'll see that the XAML-files you created are found intact in the resources of the dll.

Upvotes: 0

Related Questions