1
votes

I acknowledge the pattern for MVVM (Model, View, ViewModel) as having the 'model' (piece of data), view (interface), and viewmodel (logic between data and interface).

With Xamarin forms, there is the Page.Xaml, and Page.Xaml.cs.

If my entire interface is coded in xaml with no GUI written in the code behind file..

Does this mean that the Page.xaml.cs to a xamarin forms page is the ViewModel?

My current structure is as follows..

  • Page (GUI)
  • Page Code behind (stores a reference to the object we are working with, if any. Also has logic for updating fields based on values in the model etc.)
  • The model itself (fields and any methods to manipulate the model)
2

2 Answers

4
votes

No.

The relatively easy way to determine this is: can the code be tested in a unit test?

The code behind in a Page.xaml.cs requires the page to be instantiated in order to be tested. The resulting complexity and dependencies make it impossible to call it a unit (isolated) test.

One of the goals of MVVM is improving the testability of the code. Code behind is hard to test.

Business logic belongs in the ViewModel and Model. The code behind 'finds' its ViewModel in its DataContext and should only propagate values through bindings and execute commands on the ViewModels. There should be no business logic because it cannot be tested.

Code-behind should only be concerned with display logic and user interaction (drag drop, click, sort, etc.) in most cases this logic will already be available in existing controls and might be configured through XAML.

0
votes

MVVM is Model View ViewModel. MVVM is software architecture pattern which helps to decouple the view, data, and business logic. Any architecture pattern helps to provide better maintainability, testability to your code. Now your question "Does this mean that the Page.xaml.cs to a Xamarin Forms page are the ViewModel" is not. It is not ViewModel.
To get more information use this (https://developer.xamarin.com/guides/xamarin-forms/xaml/xaml-basics/data_bindings_to_mvvm/).