ispiro
ispiro

Reputation: 27633

C# and XAML in WPF – How do they work together?

In winforms – there's a message pump waiting for an event to happen – when that happens – the appropriate event handler in C# is called.

In WPF there's also XAML. When is that executed? Does the C# code call it or does it call the C# code? In other words: Does an event trigger C# code to run, or does it trigger XAML to be executed?

Upvotes: 5

Views: 2566

Answers (3)

ispiro
ispiro

Reputation: 27633

It seems (please correct me if I'm wrong) that WPF is not really different in the flow of things from winforms. The message pump will call C# event handlers, and the initialization of the form will be done in an InitializeComponent method.

The difference is just that the InitializeComponent method of a WPF form will include parsing an XAML file, so essentially, the developer is describing the initial appearance of the form using XAML instead of C#.

(Of course "C#" can be interchanged here with "VB".)

Upvotes: 2

Kenneth Ito
Kenneth Ito

Reputation: 5261

Here's some info about the wpf application and it's "lifecycle". http://msdn.microsoft.com/en-us/library/ms743714.aspx

And here's some info on InitializeComponent and the role it plays tying into Xaml parsing. What does InitializeComponent() do, and how does it work in WPF?

I'll see if I can find a more official post about the Xaml parsing.

From http://msdn.microsoft.com/en-us/library/aa970678.aspx

" The XAML file is parsed by the markup compiler.

A compiled representation is created for that XAML and copied to the obj\Release folder.

A CodeDOM representation of a new partial class is created and copied to the obj\Release folder.

In addition, a language-specific code file is generated for every XAML file. For example, for a Page1.xaml page in a Visual Basic project, a Page1.g.vb is generated; for a Page1.xaml page in a C# project, a Page1.g.cs is generated. The ".g" in the file name indicates the file is generated code that has a partial class declaration for the top-level element of the markup file (such as Page or Window). The class is declared with the partial modifier in C# (Extends in Visual Basic) to indicate there is another declaration for the class elsewhere, usually in the code-behind file Page1.xaml.cs. "

Upvotes: 1

brunnerh
brunnerh

Reputation: 184296

A XAML-Parser parses it and creates the respective CLR objects from it, that is about it.

Upvotes: 0

Related Questions