If your are a WPF Application Developer or learning WPF,you have seen that using XAML we can solve my complex graphical problem very easily.When you create a New WPF project it will creat a XAML and .cs/.vb file for you.Most of the time you define you UI in XALM file and your Business logic in .cs/.vb file.
Do you ever think, how compiler work when you compile or run your application. When this question arise in my mind it try to find out the answer.
I was studying a book on WPF named pro-wpf-in-c-2008-windows-presentation-foundation-with-net-3-5-second-edition last night and find out my answers that i was looking for. I want to share it with every one.
When you compiling a WPF application visual Studio uses a two-stage compilation process.
- In the first step compiler compile the XAML files into BAML using the xamlc.exe. 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).
Here is the Window1.xaml file:
- In the second step a partial class is created for your window at the same time, using the language of your choice. For example, if you’re using C#, the compiler will create a file named Window1.g.cs in the obj\Debug folder. The g stands for generated.
- 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.
namespace WpfApplication {
/// Window1
public partial class Window1 : System.Windows.Window, System.Windows.Markup.IComponentConnector
{
///The Control fields.
internal System.Windows.Controls.TextBox textBox1;
internal System.Windows.Controls.Button button1;
private bool _contentLoaded;
///Load the BAML.
[System.Diagnostics.DebuggerNonUserCodeAttribute()]
public void InitializeComponent()
{
if (_contentLoaded)
{
return;
}
_contentLoaded = true;
System.Uri resourceLocater = new System.Uri("/WpfApplication;component/window1.xaml", System.UriKind.Relative);
System.Windows.Application.LoadComponent(this, resourceLocater);
}
///Hook up each control.
[System.Diagnostics.DebuggerNonUserCodeAttribute()] [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)]
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Design", "CA1033:InterfaceMethodsShouldBeCallableByChildTypes")]
void System.Windows.Markup.IComponentConnector.Connect(int connectionId, object target)
{
switch (connectionId)
{
case 1:
this.textBox1 = ((System.Windows.Controls.TextBox)(target));
return;
case 2:
this.button1 = ((System.Windows.Controls.Button)(target));
return;
}
this._contentLoaded = true;
}
}
}
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.
Application.LoadComponent() method.
When the XAML-to-BAML compilation stage is finished, Visual Studio uses the appropriate language compiler to compile your code and the generated partial class files. In the case of a C# application, it’s the csc.exe compiler that handles this task. The compiled code becomes a single assembly (WpfApplication.exe) and the BAML for each window is embedded as a separate resource.
Resources:
Matthew MacDonald, pro-wpf-in-c-2008-windows-presentation-foundation-with-net-3-5-second-edition
.jpg)