Lunch & Learn session on Xamarin.Forms – building app logic with Xamarin.Forms and MVVM

You probably already know about our OSI Lunch & Learn sessions. Over time, this became a good way for people at the company to become familiar with the work of different teams, to go deeper in specific topics related with our work and projects and to share ideas and knowledge.

You may also remember our first Lunch & Learn session which had the topic “What is Xamarin.Forms and how does one get into it”.

So, here we are again – ready for the second Lunch & Learn session on Xamarin.Forms at OSI. Anyone who is interested and wants to learn more about how to build app logic with Xamarin.Forms and MVVM is invited. This time the session is on what approaches we can use to implement logic in our app, user interaction and a very quick overview of MVVM.

And before I get to the point, let me share something personal with you. There were some technical difficulties while recording the session, so the video below was done after it. I had to re-record it in front of the mirror without an audience. It was great fun! You should try it yourself sometime.

Now, let’s get back to the contents of the session. It has the following three main parts:

  • Data Binding
  • Commands and interaction
  • MVVM overview using Prism

So, let’s start.

How to achieve Data Binding

Data Binding is the process of binding properties of your UI controls to data objects. There are some specifics I note about how to achieve this in Xamarin.Forms, along with the various Binding Modes you can have. All of that is shown in the video and the examples, but the most important thing you need to remember about Data Binding is to always mind the BindingContext.

User interactions with Commands

In the second part of the session, I talk mainly about how to use Commands to execute some logic based on user actions. I focus on the approach of using Commands as that comes in handy if we want to achieve a proper architecture for our app. This becomes evident later, when we go over MVVM. Basically we have several points of note here:

  • Set a command to a control that supports it
  • Use Behaviors to pass events to Commands
  • CommandParameters
  • Gesture Recognizers
  • Add commands to your own controls

How to use the MVVM pattern with Prism

Lastly, I show some very crude basics of MVVM. A quick explanation of what it is and why we use it, and then I jump into the demo. For that, I show how to easily start using the MVVM pattern with Prism. It’s a very good library that gives us all parts of MVVM out of the box. We have Dependency Injection, we have a custom Navigation Service that’s URL-based, and we also have ViewModel wiring. There are quite a few other things Prism gives us, like Behaviors, the EventAggregator, a service for creating Dialogs and so on, but these can be found in their docs and are quite easy to use. I show a pretty basic implementation during the session, which is definitely not enough to cover all that Prism has to offer, but that can be left for another, future, session. I recommend that anyone interested in digging deeper follows this guide, as it is quite comprehensive.

That’s it for the overview of the session – below you can find the recording of it, as well as the presentation and the link to GitHub with the demos used. If any questions come up – leave a comment or send me an email. I’d be happy to try and answer any questions, as long as I don’t get flooded. Links in this blog post and in the links at the end of the presentation should help with any questions that pop up. Stack Overflow and the Xamarin.Forms forum should have answers for almost anything.

Happy coding, everyone!

GitHub link

[embeddoc url=”” download=”all” viewer=”microsoft”]

This post was written by Konstantin Severi