Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xamarin & Visual Studio, Windows Phone & ASP.NET: Is a true single class library possible?

I've currently built a service layer and API which I'm consuming from an ASP.NET website, Windows Phone app, and a Windows 8 metro app - the key to all of them being a project containing model classes which is a portable class library project, and is shared as a reference between all of them. This architecture works fine and without any issues.

However, I've recently started experimenting with Xamarin - specifically the iOS functionality (with a view to moving onto Android later). My initial assumption was that I would be able to utilise this model library as I do with the other projects - however, I've since realised this this isn't the case.

When I first added a reference to my model class library to my Xamarin.iOS project (in Visual Studio), I was greeted by the error:

'The current project does not support references to Portable Library projects'.

Strange - as I thought this would be a scenario that would be supported. I found a post here which suggested a fix that forced Visual Studio to detec the Monotouch (Xamarin) framework as supporting Portable Library Projects. Seemed to be exactly what I needed - but no. When I added the reference again, I was greeted by a different error:

'The current project's target framework is not compatible with that of the Portable Library project'.

And it suggests I change the target framework in the project properties. Except I can't, as the Xamarin iOS project needs to support Monotouch (and I can't change it to anything else from the GUI anyway). So I tried to change the model library's properties instead - maybe I can do something there. And it looked like I could. I can check Monotouch (Xamarin) as a supported platform here (where Windows phone, Windows store, etc are already checked), but it won't let me save. It just shows the message:

'There is no additional functionality that is supported by the target frameworks you have selected'

After some research, I discovered that the only way Xamarin suggest to share code is to use separate projects with linked code files (so in effect, a pseudo-single code base. Which I have tried and works - but it seems a hack. See here:

http://docs.xamarin.com/guides/cross-platform/application_fundamentals/building_cross_platform_applications/sharing_code_options

So is this the only option? Or is there any way to have a true single class (model) library across all platforms, including Microsoft and Xamarin?

like image 962
stevehayter Avatar asked Apr 07 '13 21:04

stevehayter


1 Answers

It is currently possible to use Portable Library Projects - e.g. profile 104 - across all of .Net4.5, WP, WinRT, Xamarin.Android and Xamarin.iOS

It is also currently possible to use the output binary assemblies across .Net4.5, WP, WinRT and Xamarin.Android - but not current against Xamarin.iOS.

This situation is changing 'very soon' now with Xamarin adopting full PCL support - see http://slodge.blogspot.co.uk/2013/02/the-future-is-portable.html and listen to http://hanselminutes.com/362/ios-and-android-apps-with-xamarin-studio-20-and-miguel-de-icaza

I personally deliver MvvmCross (and a lot of apps that use MvvmCross) using PCLs - and have done for the last 6 months - but there is some setup pain involved in this currently. For some info see my blog:

  • http://slodge.blogspot.co.uk/2012/12/cross-platform-winrt-monodroid.html
  • http://slodge.blogspot.co.uk/2013/03/xamarinios-with-mvvmcross-in-vs2012.html

I do hope to provide an update on this in the very near future - it should work 'out of the box' soon.

like image 131
Stuart Avatar answered Oct 16 '22 03:10

Stuart