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?

ispiro
  • 26,556
  • 38
  • 136
  • 291
  • The MSDN has an interesting article on building a wpf application: http://msdn.microsoft.com/en-us/library/aa970678(v=vs.100).aspx – Nick Babcock Jun 17 '12 at 20:17

3 Answers3

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

ispiro
  • 26,556
  • 38
  • 136
  • 291
  • So basically XAML is parsed at runtime? I assumed it is compiled to IL/native bits at compile time. – nawfal Apr 28 '16 at 13:00
  • Got the answer here: http://blogs.microsoft.co.il/tomershamam/2007/05/25/compiled-xaml-baml-not-il/ – nawfal Apr 28 '16 at 13:42
  • @nawfal the link is broken – Sisir Dec 04 '18 at 04:27
  • 1
    @Sisir works for me though.. Anyway try a cached version: http://webcache.googleusercontent.com/search?q=cache:blogs.microsoft.co.il/tomershamam/2007/05/25/compiled-xaml-baml-not-il/ – nawfal Dec 04 '18 at 06:45
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. "

Community
  • 1
  • 1
Kenneth Ito
  • 5,201
  • 2
  • 25
  • 44
  • 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? – ispiro Jun 17 '12 at 20:31
  • 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? – ispiro Jun 17 '12 at 20:33
  • Umm... somewhat. Did you read Nick's post? http://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. – Kenneth Ito Jun 17 '12 at 20:45
  • You can easily view the generated classes yourself if you're interested. They're in the obj folder. – Kenneth Ito Jun 17 '12 at 20:54
0

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

H.B.
  • 166,899
  • 29
  • 327
  • 400
  • Parses what?​​​​​​​​​​​​​​​​​​​​​​​​​ – ispiro Jun 17 '12 at 20:11
  • 1
    Is that a question or irony? And when does it get parsed? Does C# call the parser? (That _is_ my question.) – ispiro Jun 17 '12 at 20:13
  • @ispiro: The parser is written in C#, in System.Xaml.dll. – SLaks Jun 17 '12 at 20:13
  • 1
    @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? – H.B. Jun 17 '12 at 20:14
  • Do I understand correctly that events trigger C# code only? And XAML is "executed" only when a form is shown? – ispiro Jun 17 '12 at 20:25
  • @ispiro: No, you think too language dependent. In any case, the XAML is only parsed once, be it at compile time, on application startup or at any point at run-time. – H.B. Jun 17 '12 at 20:51
  • 1
    I dont think they are CLR objects. They are not compiled to IL. More like CLR objects reading binary markup at runtime. Source: http://blogs.microsoft.co.il/tomershamam/2007/05/25/compiled-xaml-baml-not-il/ – nawfal Apr 28 '16 at 13:33
  • @newfal: Well, my wording was a bit inaccurate, the parser does not create the CLR objects directly, it just creates a parse tree of some form, according to which the WPF app creates objects (framework elements and the like). – H.B. Apr 28 '16 at 14:14