3

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.

Zach Smith
  • 8,458
  • 13
  • 59
  • 133

3 Answers3

8

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?

Community
  • 1
  • 1
mm8
  • 163,881
  • 10
  • 57
  • 88
3

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

mlg
  • 1,162
  • 1
  • 14
  • 32
-2

For an application using a MVVM (Model View ModelView) implementation:

  • View: *.xaml
  • ViewModel: *.xaml.cs
tdalz
  • 1