James Montemagno
James Montemagno

Live, Love, Bike, and Code.

Live, Love, Bike, and Code

Share


Tags


Xamarin + PCL + MAC Xamarin Studio + Visual Studio #LetsDoThis

This blog is now obsolete as official PCL support has been added to Xamarin.Android and Xamarin.iOS for both Visual Studio and Xamarin Studio. There is no longer a need to do any file copying or anything fancy, simply hit File->New Project -> PCL. I have a webinar: you can find here

So last night at the Xamarin drinkup in Bellevue there was a lot of discussion about PCL and also bout MVVM frameworks such as the one I am using MVVMCross (Mvx). While PCL is “sorta”/“kinda”/“it should just work” on the Xamarin platforms there still seems to be a lot of confusion and a lot of issues setting up. So this morning I am going to do a quick overview of What is PCL, Why you should use PCL and how to get it setup inside of Visual Studio on your PC and also inside of Xamarin Studio on your Mac. I blame this entire article on Frank Krueger so thank him on twitter and buy is amazing apps.

What:

PCL stands for Portable Class Libraries. The whole concept was that different platforms are all crazy with different code such as Xbox, Windows Phone, Silverlight. So why not write a lot of your core logic in a language that any of them can understand. A subset of the full framework that these platforms can consume.

Why:

The what basically summarizes all of this to be honest, but think about writing one core logic for your app with one type of project that anything can consume. No more project or file linking involved, simply add the PCL core project to your client apps and boom tons of functionality for you. Now I wont get into it, but throw in an MVVM framework into this mix and everything should/kind of is amazing. The biggest issue that I have seen from PCL is that not everything supports it, it is only for microsoft platforms, and until recently with .net 4.5 the BCL (which you can look up and I am not going to go into details) weren’t a PCL. However!!! Seeing that Xamarin.iOS and Xamarin.Android allow you to code in C# with the .Net Framework AND that they allow you to use PCL projects…. 1 code base for iOS, Android, Windows Phone, and Windows Store apps.

More Info Before We Begin

Now how PCL stuff works is everything is a Profile. Profiles basically saying hey if you are this profile you can use these dlls. This is nice so PCLs can distinguish between different platforms. I target Profile104. Which allows WP, Windows Store, Android, and iOS apps.

The current issues

I don’t have a 100% work around for getting the BCL nuget package working so async/wait and .net 4.5 will have to wait a bit until Xamarin and Microsoft figure everything out. So for right now I am living in the .net 4.0 world and still loving it.

Visual Studio

My setup is Visual Studio 2012 on Windows 8 Pro with the latest Alpha release of everything Xamarin.

1.) Go to “C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETPortable\v4.0\Profile\Profile104\SupportedFrameworks”

2.) Add these 2 xml documents which will allow MonoAndroid and MonoTouch to be targeted PCL platforms.

3.) Optional: you could add these to other platforms as well if needed, i use 104 because it works.

4.) Start up Visual Studio and create a new Portable Class Libary project.

5.) When you see a pop up for platforms simply select:

After that you should be on your way. Create a new iOS app, android app and just add in the project as a reference. If you don’t want to use the xml files there is another way that my good friend Tomasz Cielecki uses which he blogged about.

Mac:

So things are a bit trickier here. My current setup is the latest version of xamarin.studio (alpha), Mac OSX 10.7.5, XCode 4.6.1.


1.) The biggest issue is that the PCL files that Visual Studio references aren’t actually on your mac or installed when you install Xamarin Studio. You can read about the problem here.

2.) So let’s move them over. If you have a PC there is a folder you want to copy which is located at: “C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETPortable”

3.) Copy this entire folder… or download it RIGHT HERE if you don’t have a PC.

4.) You need to copy it over onto your Mac to this directory:
/Library/Frameworks/Mono.framework/External/xbuild-framework/

So all together inside of
/Library/Frameworks/Mono.framework/External/xbuild-framework/ should be .NetPortable with .Net4.0 and .net4.5 inside and also MonoTouch.

If you are on a linux box you can copy to: $(libdir)/mono/xbuild-framework

5.) Alright so you should be good to go, but you would need to now create a new PCL project in Xamarin Studio. So Create a new Solution

6.) I like to name “MyApp.Core” and the Solution Name “MyApp”

7.) Once the project is created simply Right Click on the Project and go to: Options -> General and select Mono for Android and MonoTouch.

8.) If for some reason this doesn’t stick or tells you that Profile 104 isn’t installed it is lying and you will need to manually modify your .csproj in a text editor and simply ensure these 3 lines are in your main Property Group:

TargetFrameworkVersion: v4.0
TargetFrameworkProfile: Profile104
FileAlignment: 512

**Sorry about the xml tags missing tumblr is weird and I need to fine a better code plugin.

And if for some reason that doesn’t work… just use my pre-made template you can download from HERE.

So I think that just about wraps it up. If you have issues leave a comment, or hit me up on twitter @JamesMontemagno

Copyright © James Montemagno 2013 All rights reserved. Privacy Policy

View Comments