Developer's Academy: Relationship Between .XAML and .XAML.CS Files

Tuesday, 2 September 2014

Relationship Between .XAML and .XAML.CS Files

In Visual Studio’s Solution Designer, you can see that the XAML files have an arrow which means that we can expand them to reveal a C# file by the same name, the only difference is that it has a .cs file name extension appended to the end. If you look at the .cs version of the file, you’ll see that it defines a MainPage class, and furthermore, it defines it as a PARTIAL class.

       public sealed partial class MainPage : Page

The other half of the equation is defined in lines 1 & 2 of the MainPage.xaml:

<Page
        x:Class="HelloWorld.MainPage"
        ...

While it doesn't use the term Partial like its procedural counterpart, it does indicated the relationship between the two.

Why is this Important? 

This relationship means that the compiler will combine the output of the MainPage.xaml and the MainPage.xaml.cs files into a SINGLE CLASS. This means that they are two parts of a whole. That’s an important concept … that the XAML gets compiled into Intermediate Language just like the C# gets compiled into Intermediate Language and they are both partial implementations of a single class. This allows you to create an instance of a class in one file then use it in the other file. This is what allows me to create an instance of the Button class called clickMeButton in XAML and then call its methods or set its properties in C#.

No comments:

Post a Comment