left-icon

Xamarin.Forms Succinctly®
by Alessandro Del Sole

Previous
Chapter

of
A
A
A

CHAPTER 2

Sharing Code Among Platforms

Sharing Code Among Platforms


Xamarin.Forms allows you to build apps that run on Android, iOS, and Windows from a single C# codebase. This is possible because, at its core, Xamarin.Forms allows the sharing among platforms of all the code for the user interface and all the code that is not platform-specific. Starting with Visual Studio 2019, code sharing happens only through .NET Standard. This chapter explains how code sharing works in Xamarin.Forms, also providing suggestions on how you can adopt .NET Standard to share your own code and not only the user interface.

Sharing code with .NET Standard

The .NET Standard provides a set of formal specifications for APIs that all the .NET development platforms, such as .NET Framework, .NET Core, and Mono, must implement. This allows for unifying .NET platforms and avoiding future fragmentation. By creating a .NET Standard library, you will ensure your code will run on any .NET platform without the need to select any targets. This also solves a common problem with portable libraries, since every portable library can target a different set of platforms, which implies potential incompatibility between libraries and projects. Microsoft has an interesting blog post about the .NET Standard and its goals and implementations that will clarify any doubts about this specification.

At the time of writing, version 2.1 of .NET Standard is available, though new Xamarin.Forms projects use version 2.0 by default. .NET Standard libraries are certainly not exclusive to Xamarin. In fact, they can be used in many other development scenarios. For example, a .NET Standard library could be used to share a Model-View-ViewModel architecture between a WPF project and a UWP project.

The most important characteristics of .NET Standard libraries are:

  • They produce a compiled, reusable .dll assembly.
  • They can reference other libraries and have dependencies such as NuGet packages.
  • They can contain XAML files for the user interface definition and C# files.
  • They cannot expose code that leverages specific APIs that are not available on all the platforms targeted by a specific .NET Standard version.
  • They are a better choice when you need to implement architectural patterns such as MVVM, factory, inversion of control (IoC) with dependency injection, and service locator.
  • With regard to Xamarin.Forms, they can use the service locator pattern to implement an abstraction and to invoke platform-specific APIs through platform projects (this will be discussed in Chapter 9).
  • They are easier to unit test.

For example, it’s only possible to use a component of the .NET Standard library for both a WPF and UWP application to access the location sensor of a device if the component is supported by both application types. Normally, you would create a .NET Standard library project manually, and then add the necessary references to and from other projects in the solution. In the case of Xamarin.Forms, Visual Studio 2019 automatically generates a .NET Standard project that is referenced by the platform projects in the solution, and that has a dependency on the Xamarin.Forms NuGet package.

.NET Standard with Xamarin.Forms

In Xamarin.Forms solutions, the .NET Standard library contains all the cross-platform code and all files that are required to support the shared code. For example, it contains the user interface definition, the imperative code that handles and reacts to the user interface, images that must be embedded in the app package, resource files, and configuration files. As an implication, platform-specific code will not be included in the .NET Standard library; instead, it will be placed in the Android, iOS, and UWP projects.

For example, the way an app accesses a local SQLite database is platform-specific, so the code for this will not be in the .NET Standard library. Chapter 9 discusses platform-specific code in more detail. For now, it is important to focus on the .NET Standard project, because in the next chapters you will write all the code inside this project, and it will run on Android, iOS, and UWP from this single codebase.

By default, Xamarin.Forms solutions target .NET Standard 2.0. In case you need to address compatibility issues with third-party libraries, you can change the .NET Standard version in the project properties. To accomplish this, right-click the project name in Solution Explorer, and then select Properties. As you can see in Figure 18, you can change the version of .NET Standard in the Target framework drop-down menu.

Changing .NET Standard version

Figure 18: Changing .NET Standard version

When you change the version, Visual Studio will update the project accordingly and will reload the solution.

Adding .NET Standard libraries

.NET Standard libraries make it easier to reuse code across platforms and solutions. You could create a reusable library for multiple solutions, for example, if you are implementing a service library to call Web APIs, or simply add a .NET Standard library to one solution to share more code across platforms.

To add a .NET Standard library to a Xamarin.Forms solution, right-click the solution name and then select Add > New Project. In the Add a new project dialog, you can filter the list of templates by language by selecting C# from the drop-down menu (see Figure 19).

Adding .NET Standard libraries to a Xamarin.Forms solution

Figure 19: Adding .NET Standard libraries to a Xamarin.Forms solution

Locate and select the Class Library (.NET Standard) project template (see Figure 19). Simply click Next, provide a name for the new project, and click Create. After a few seconds, the new project will be added to the solution.

You will need to add a reference to this new project from each of the projects in the solution that will use its types. Discussing all the APIs provided by .NET Standard is not in the scope of this book, but the official documentation provides the proper information. For example, you will be able to click on the version number of each .NET Standard implementation to get a list of exposed APIs.

Chapter summary

This chapter discussed code-sharing in Xamarin.Forms and explained how .NET Standard is leveraged to share user interface files and platform-independent code. .NET Standard libraries produce reusable assemblies, allow for implementing better architectures, and cannot contain platform-specific code.

.NET Standard libraries represent the present and future of code-sharing across platforms, are based on a formal set of API specifications, and they make sure your code will run on all the platforms that support the selected version of .NET Standard. Now that you have basic knowledge of how code sharing works and why it is fundamental, in the next chapters you will start writing code and building cross-platform user interfaces.

Scroll To Top
Disclaimer
DISCLAIMER: Web reader is currently in beta. Please report any issues through our support system. PDF and Kindle format files are also available for download.

Previous

Next



You are one step away from downloading ebooks from the Succinctly® series premier collection!
A confirmation has been sent to your email address. Please check and confirm your email subscription to complete the download.