You might be asking yourself at this point, how do I set up my solution in Xamarin Studio to handle shared code and also have platform-specific projects? Xamarin.iOS applications can only reference Xamarin.iOS class libraries, so setting up a solution can be problematic. There are several strategies for setting up a cross-platform solution, each with its own advantages and disadvantages.
Options for cross-platform solutions are as follows:
#if IPHONE
. You can also reference different libraries on Android versus iOS.To understand each option completely and what different situations call for, let's define a solution structure for each cross-platform solution. Let's use the product search example used earlier in the chapter and set up a solution for each approach.
To set up file linking, perform the following steps:
ProductSearch.Core
, and name the solution ProductSearch
.Product
, ProductRepository
, and ProductViewModel
classes to the project used earlier in the chapter. You will need to add using System.Threading.Tasks;
and using System.Linq;
where needed.ProductSearch.iOS
.ProductSearch.Droid
.Core
to both the iOS and Android projects.ProductSearch.Core
project.When all is done, you will have a solution tree that looks something like what you can see in the following screenshot:
You should consider using this technique when you have to reference different libraries on each platform. You might consider using this option if you are using MonoGame
, or other frameworks that require you to reference a different library on iOS versus Android.
Setting up a solution with the cloned project files approach is similar to file linking, except that you will have to create an additional class library for each platform. To do this, create an Android library project and an iOS library project in the same ProductSearch.Core
directory. You will have to create the projects and move them to the proper folder manually, then re-add them to the solution. Right-click on the solution and navigate to Display Options | Show All Files to add the required C# files to these two projects. Your main iOS and Android projects can reference these projects directly.
Your project will look like what is shown in the following screenshot, with ProductSearch.iOS
referencing ProductSearch.Core.iOS
and ProductSearch.Droid
referencing ProductSearch.Core.Droid
:
3.144.93.222