Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 77050

Xamarin and the MVVM pattern

$
0
0

Hello everyone,

I'm working on my newest app with Xamarin, not my first one though, and this time I plan to strictly follow the MVVM pattern as I kind of fall in love with the idea behind after I finally took the time to understand what it actually brings :D

I watched tons of youtube videos and read many articles from general MVVM explanation to MVVM in Xamarin and so on.
However, often they are not really doing MVVM it's more like VVM (View-ViewModel) or VM (View-Model).
I'm not 100% sure if what they do there is correct and strictly following the MVVM pattern would be overkill as not needed at those points or if it is just not correct.

In this post I would like to clarify my understanding and I hope others will join and help me :)

As it is the easiest I would go with a simple example of what I find on the web and why I think it is not MVVM but VVM.

There is a ContentPage for a Login, as we not focus on UI, for simplicity lets say like that.

<StackLayout>
        <!-- Place new controls here -->
        <Entry x:Name="Username" Placeholder="Username" Text="{Binding Username}"/>
        <Entry x:Name="Password" Placeholder="Password" Text="{Binding Password}"/>
        <Button x:Name="Submit" Text="Submit"/ Command="{Binding SubmitCommand}" />
</StackLayout>

From my understanding, the ContentPage represents the View within the MVVM pattern, so the underlying .cs file that comes with the .xaml, is also a view and should not have any business logic or data or whatever stored.
As you can see we bind some values to Text of Entries and Command of Button.
So all the .cs of the xaml does is defining this BindingContext which is of type SampleViewModel.

public partial class MainPage : ContentPage
{
        public MainPage()
        {
            InitializeComponent();
            BindingContext = new SampleViewModel();
        }
}

The SampleViewModel on the other hand has to define a string Username, a string Password as well as the command, So it will look like this:

public class SampleViewModel
{
        public String Username { get; set; }
        public String Password { get; set; }
        public Command SubmitCommand { get; }

        public SampleViewModel()
        {
            SubmitCommand = new Command(() => { /*Do something here*/});
        }
}

And that should be it. This is the point to which a lot of Tutorials lead but I'm wondering, where is the Model?
I could move the Username and Password String into a model and reference it into the SampleViewModel but I don't really see the point of that here as it doesn't bring any advantage in this rather simple example.

So I'm wondering, is it fine to roll like that?
Are Models only meant for more complex scenarios?
For example when having a Listview displaying contacts a model can describe a single contact but it is not required to have a model that at the same time describes some basic things within the ContentPage. An example that comes to my mind would be a search bar at the top to find a contact. The value entered there could again go into the ViewModel as a String, but no need to have a Model that holds this String.

I think that's it for the start of the discussion :) am I on the right track, is it correct or is it supposed to be otherwise?

Thank you all!


Viewing all articles
Browse latest Browse all 77050

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>