Inside Xamarin, How Shared Code Is Organised?

Xamarin has its influence on various programs such as Android. Microsoft and iOS. It helps in saving a lot of time for programming, testing and maintenance. They offer an option to share the codebase. It takes a lot of time to write code, find a bug and fix them. They have some platform for specific code like the native UI code or the controller code that takes you from UI controls to the logic specific features.

Web services can be accessed through the shared code. Firstly To feature that required platform-specific code, it is a good idea to check if an abstraction exists then you can create abstractions over it and call it from the shared code.

These are the abstraction that would be helpful in saving a lot of your time:

.NET Foundation
Xamarin Component Store source Plugins0
Xamarin.Mobile (common mobile services)
Now. How are shared code organised?

Shared Projects

It is Xamarin’s new type of project where a common code is settled. While compiling, all files are shared project are compiled into each other target platform. Also, add a reference to shared projects in each of the platform projects that you have.

To write shared codes there are few techniques that we should look into.

Conditional Compilation

You can create blocks of code with c#. For every platform, we can define these conditional compilation symbols through project build settings.

On the other side, it is difficult to see what gets compiled and if a change breaks another target build without compiling for it. Then the code will be difficult to manage.

Class Mirroring

This defines a class in each of the platforms but keeps the class name and method same. Then use the same in the shared code. Its implementation will come from its respective target project.

Partial Classes and methods

C# has a partial keyword that defines partial classes and methods. IT can be split across multiple source files.
A partial method tells the compiler that the method is optional. A code is emitted to call the method at compile time if the method definition is found in the respective project.

This is helpful if you want to do something specific on a platform and not on any other platform.This would help you get an idea how Xamarin coding is processed and make it easy to maintain and extend. Consult us for any query, we would be glad to help you.

Leave a Reply

Your email address will not be published. Required fields are marked *