开发者

Can you view XAML as "regular" .net code (c#\vb.net)?

开发者 https://www.devze.com 2022-12-29 02:47 出处:网络
There are times when I fi开发者_如何学运维nd some example XAML that I want\\need to do in code (c#\\vb.net).

There are times when I fi开发者_如何学运维nd 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.


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.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消