Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between .xaml.cs and ViewModels/ViewModel.cs?

I have been following this tutorial on MVVM pattern using caliburn micro

https://www.youtube.com/watch?v=laPFq3Fhs8k

What is the difference between .xaml.cs and ViewModels/ViewModel.cs ?

I see we put our code for the shellview in ShellViewModel.cs, but when I, for example, choose an event handler for a Button, visual studio takes me to .xaml.cs and not to the ViewModels/ViewModel.

Do I have to stick to one or each file has a different role/responsibility? ( considering that I want my code to still be following MVVM pattern )

like image 330
aboudirawas Avatar asked Oct 23 '25 14:10

aboudirawas


1 Answers

Files ending in .xaml.cs are what is called the View's "code-behind". Anything inside this file is tightly coupled with the View.

If you want to follow the MVVM pattern, you will code almost all of the interaction of the UI with your Models (example: click of a button change some data or get some data from the database) on the ViewModels (.cs files), which are independent from the Views.

The only link between the View and the ViewModel is that the ViewModel is the DataContext of the View. In other words, the View knows about the ViewModel, but that ViewModel does not know anything about the View.

Event Handlers are not generally used in a MVVM world. You need to use Commands. But that is a whole other question/answer.

The important thing to keep in mind is that MVVM comes to address, mainly, two things: the Separation of Concerns (UI vs UI Logic) and Testability of your software.

like image 126
Daniel Marques Avatar answered Oct 25 '25 03:10

Daniel Marques



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!