views:

54

answers:

3

There are times when I find some example XAML that I want\need to do in code (c#\vb.net).

I assume at some point the XAML becomes code, or at least IL.

So my questions:

  • Am I correct in assuming that XAML is converted to IL? (or if not IL what does it become?)

  • If the above is correct, when does XAML become IL (or whatever it becomes)?

  • Is there some way to see the XAML in as "code"

Thanks.

+1  A: 

WPF and Silverlight treat XAML differently; neither convert XAML to IL. WPF's markup compiler converts XAML to a compiled form called BAML that is a binary version of the XAML. Silverlight leaves the XAML as plain text (compressd in the .XAP) and parses it at runtime.

chuckj
+3  A: 

No, XAML does not compile into IL, that gets done at runtime. The best way to think about it is as a way to compose an application from components.

For the majority of things you can replicate in C# what you do in XAML, however there is a small number of things that is available in XAML that's not in C# and vice versa. Charles Petzold at some point said that ostensibly, XAML looks like XML, but it's actually not, it's a language of its own.

For example this XAML code:

<Grid>
  <TextBlock Text="Something" />
</Grid>

Is equivalent to the following C# code. This will get done in C# at runtime and short of setting a breakpoint in a particular component's constructor, there isn't much you can do to figure out what executes at runtime.

var grid = new Grid();
grid.Children.Add(new TextBlock{Text = "Something"});

I am sure there is a solution to your problem, but not as an answer to this particular question. Can you give more details on your problem and we can help you understanding it.

Igor Zevaka
A: 

Is there some way to see the XAML in as "code"

If you are talking about the hierarchy of controls in xaml, then you may use myControl.Parent. You can "see in code" how the controls in xaml are nested. You will also get/set their properties.

Arvin Meralpis