Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the connection between .xaml and .xaml.cs files

Tags:

c#

xaml

For a view, there are corresponding .xaml and a .xaml.cs files. How are these files related?

I'm super new to xaml - I think I see dynamic placeholders in the .xaml files, but with explicit references to files as sources other than the corresponding .cs file. I think I see controller-view behavior, where user interaction triggers state changes on the view comparable to Javascript/CSS UI interactions.

like image 378
Zach Smith Avatar asked Nov 01 '25 12:11

Zach Smith


2 Answers

The XAML file (.xaml) and the corresponding code-behind file (.xaml.cs) are two partial definitions of the same class.

Partial Classes and Methods (C# Programming Guide): https://msdn.microsoft.com/en-us/library/wa80x488.aspx

The InitializeComponent() method that is called in the constructor of the code-behind class at runtime locates a URI to the compiled XAML file and passes it to a LoadComponent() method that parses the BAML, i.e. the compiled XAML, and creates instances of the elements that you have defined in your XAML markup. Please refer to the following link for more information about this.

What does InitializeComponent() do, and how does it work in WPF?

like image 130
mm8 Avatar answered Nov 04 '25 02:11

mm8


.xaml is the designer file and .xaml.cs is the code-behind file where you write the business logic instead of inline code in .xaml. This provides more flexiblity of code separation and code readability.

like image 27
mlg Avatar answered Nov 04 '25 04:11

mlg



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!