5

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?

1

3 Answers 3

2

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".)

Sign up to request clarification or add additional context in comments.

4 Comments

So basically XAML is parsed at runtime? I assumed it is compiled to IL/native bits at compile time.
@nawfal the link is broken
@Sisir works for me though.. Anyway try a cached version: webcache.googleusercontent.com/…
1

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. "

4 Comments

My question is about the flow. Not about how exactly is XAML parsed. And from one of the links you posted, it seems that the XAML part is only executed when the JIT shows the form. It's sort of like the "InitializeComponent" of WPF., not in the normal course of events :) . Is that true?
In winforms there's no clear difference between InitializeComponent and the event handlers. Both are in C#. InitializeComponent is just a method like all other methods. So here in WPF it uses XAML. Is that right?
Umm... somewhat. Did you read Nick's post? msdn.microsoft.com/en-us/library/aa970678.aspx. WPF generates a partial class (regular c# or vb) based off the xaml markup which contains the control names and wired events. The partial class will match your "codebehind". So it's indirectly using the xaml I suppose.
You can easily view the generated classes yourself if you're interested. They're in the obj folder.
0

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

8 Comments

Parses what?​​​​​​​​​​​​​​​​​​​​​​​​​
Is that a question or irony? And when does it get parsed? Does C# call the parser? (That is my question.)
@ispiro: The parser is written in C#, in System.Xaml.dll.
@ispiro: C# doesn't call anything, it's a language. The internal WPF application classes and/or the compiler do. XAML may or may not be compiled into BAML. So when the parser runs is not fixed. Why would you even care?
I dont think they are CLR objects. They are not compiled to IL. More like CLR objects reading binary markup at runtime. Source: blogs.microsoft.co.il/tomershamam/2007/05/25/…
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.