Don of the Day

Don of the Day


Adventures in software development with Xamarin and the Web

Software developer, building things with code in sunny Scottsdale, AZ.

Share


Twitter


Typical Xamarin Solution Structure

Don FitzsimmonsDon Fitzsimmons

No matter what you use, Xamarin.Forms or Xamarin with native UI's, you'll start with a similar solution structure. With Xamarin.Forms, the default template will generate your solution structure with three projects. A PCL project for the bulk of your app. This is where all of your code will go. An iOS project and an Android project. These two projects are what you'll eventually deploy. When using Xamarin.Forms, you won't interact much with the platform specific projects unless you need to do something...well, platform specific.

When creating a native UI solution, like I'll be doing with my app, DownToZero, creation of the solution structure is done manually but the result is the same, at least this is how I do it. I create a PCL for the core of my app (I avoid shared libraries but that's an option too). This is where I attempt to get the most out of my code, sharing it as much as possible. This project houses my models, services, repositories and view models. I then create an iOS and Android project which both reference my core PCL. It's pretty straight forward and looks like the screenshot below:

For this app I've chosen to go with a native UI approach (more on that here). I'm also going to be using the Model-View-View-Model pattern for my UI, also known as MVVM. This approach to UI development is a great way to reuse code in conjunction with data binding. The idea is that I'll be able to keep all of my view logic in view model classes and bind those to the native views in each platform specific project.

But, there's a problem. In order for MVVM to be truly effective, data binding is needed. The platform specific projects don't support data binding. There are several frameworks that help solve this problem. The two most prominent MVVM frameworks for Xamarin development are: MVVMCross and MVVMLight. For various reasons I have chosen to use MVVMCross for this project. In short, what these frameworks do is help you achieve data binding with native UI projects and they provide you with tools to handle the application lifecycle.

Looking back at the screenshot above, the majority of my code will be in the F3.DownToZero PCL project. My platform specific projects will reference that core project and through the magic of data binding (provided by MVVMCross), I'll hopefully write very little UI code for each platform. That's the goal. This solution structure is very typical of a cross platform Xamarin app. Whether using Xamarin.Forms or a native UI approach, you'll end up with a project per platform and that's ultimately your end product.

Software developer, building things with code in sunny Scottsdale, AZ.

Comments