In this chapter, we will discuss one of the most interesting and important aspects of developing with Xamarin: cross-platform code sharing. We will cover the following topics:
One of the advantages of using Xamarin and C# is the ability to share code across your mobile apps as well as other .NET solutions. The reuse of code can provide significant productivity and reliability advantages as well as reduce many of the long-term maintenance headaches that come with long-lived apps. That's great, but anyone who has been involved in software development for a long period of time understands that reuse is not free and not simple to achieve.
There are practical aspects of reuse; the question is, "Physically, how do I package my code for reuse?" For this, we can use one of the following three methods:
There are also more strategic aspects; again the question arises, "How can I organize my code so that I can reuse more of it?" To solve this problem, we have the following options:
In this chapter, we will touch on both these aspects of reuse, but primarily focus on the practical side of reuse. Specifically, we will present two different approaches to bundle up the code for reuse.
So, what parts of our code should we try and reuse? In the work we have done on the NationalParks
apps so far, one obvious set of code stands out for reuse: the persistence code, which is the logic that loads parks from a JSON file and saves them back to the same file. In Chapter 5, Developing Your First Android App with Xamarin.Android, we moved towards a reusable solution by creating the NationalParkData
singleton. In this chapter, we will demonstrate two different methods for sharing the NationalParkData
singleton across both our projects as well as other .NET projects that might need it.
18.188.66.13