Shared projects
We are going to delve into our first technique for sharing code between different platforms. This is what Xamarin wants us to achieve, and reuse as much code as possible. The biggest disadvantage when developing natively is two different language, and we can't reuse anything.
Let's create our first shared project:
Our shared project will be used to contain the GalleryItem
model, so whatever code we include in this shared project can be accessed by both the iOS and Android projects:
In the preceding screenshot, have a look at the Solution explorer, and notice how the shared project doesn't contain anything more than .cs
code sheets. Shared projects do not have any references or components, just code that is shared by all platform projects. When our native projects reference these shared projects, any libraries being referenced via using
statements come from the native projects.
Now we must have the iOS and Android projects reference the shared project; right-click on the References folder and select Edit References:
Select the shared project you just created and we can now reference the GalleryItem
object from both projects.