0

I need a WP8 project and an Android project. Until now I have only seen a PCL called Core which is linked to all platform projects. The problem is, that I have platform specific references, which I need to include, depending on the platform. How can I handle this?

I have also seen this: https://github.com/Redth/WshLst . This project has no PCL and uses cloned projects.

Which way should I go?

Michael
  • 747
  • 2
  • 10
  • 22

2 Answers2

2

There are 2 ways to share code as you state. The PCL approach and using file linking (cloned).

With file linking, you normally use #if directives to specify platform specific features in your core. Like:

#if MONOANDROID
    var androidGlobal = this.GetService<Cirrious.MvvmCross.Droid.Interfaces.IMvxAndroidGlobals>();
    _geo = new Geolocator(androidGlobal.ApplicationContext);
#else
    _geo = new Geolocator();
#endif

With PCL projects, you would typically create an interface, then create separate platform specific implementations.

So you may have an ILocationWatcher in your PCL core project. Then you can create a WinPhone.LocationWatcher and Droid.LocationWatcher in each of the platform projects.

Usually you would use some sort of Inversion of Control (IoC) container to register the patform specific type at startup. Then your Core project would get an instance, either via dependency injection or resolving via the IoC container. Your core PCL would program strictly against the interface and not have to worry about the specific implementation.

This is how MvvmCross plugins work. Pretty much all the plugins come with a core interface and a platform specific implementation.

Kiliman
  • 18,460
  • 3
  • 39
  • 38
2

Which way you should go depends on your app and your needs.

Platform specific resources can be injected into PCL using Service Location and Plugin techniques:

Alternatively, you can use shared code with partial classes, #if, etc

Advantages and disadvantages of the 2 approaches are discussed in this excellent answer - What is the advantage of using portable class libraries instead of using "Add as Link"?

Community
  • 1
  • 1
Stuart
  • 66,722
  • 7
  • 114
  • 165