As more people are using Xamarin Forms and Xamarin Forms Labs project there are also some issues starting to arise, and a lot of questions on how start using the library. I wanted to start bloging about using the XFLabs project in your applications.
First let’s start as simple as possible, there are some features like IOC that the Labs project uses extensively that i will also explain further. But for now here’s the quickest way to get you started
1 – Install nuget packages :
You can get the bits from the Nuget official source,you just need Xamarin Forms Labs Core to get you started, and you must install the nuget packages in all your projects( main and platforms).
(for now XFLabs is recommended and tested in the pcl version of Xamarin Forms)
2 – Initialize Xamarin Forms Labs in each platform
Xamarin Forms Labs needs to be initialized so the compiler knows what dll’s it has to add to your app.
makes use of Depedency Injection for some features, is recommended that you perform a minimum setup.
On iOS you can just change in your AppDelegate.cs to this:
public partial class AppDelegate : XFormsApplicationDelegate
On Android just change your MainActivity.cs
public class MainActivity : XFormsApplicationDroid
On Windows Phone just add this lines in your App.cs:
var app = new XFormsAppWP();
app.Init(this);
3 . You are done, just use your services or controls.
var mediaPicker = DependencyService.Get<IMediaPicker>();
var result = await mediaPicker.TakePhotoAsync(new CameraMediaStorageOptions { DefaultCamera = CameraDevice.Front, MaxPixelDimension = 400 });
There are some more complex controls that do required some initialization in each plaform, we will cover that in the next post.
Make sure to add any feedback or questions on the comments section.
I just wanted to compliment you and the team on your work. I’m new to Xamarin, but I was able to get a cross-platform geolocation app working after reading this blog entry.
My only suggestion would be to add this content to https://github.com/XForms/Xamarin-Forms-Labs/wiki (if it’s not there somewhere already) because this is the only place I’ve found so far that documented critical step 2.
Thanks!
Thanks for the feedback Dan, done:
https://github.com/XForms/Xamarin-Forms-Labs/wiki
hi, having no success trying to build a simple test app, using Xam.Forms.Labs for email (EmailService). Getting null reference on DependencyService.Get () in the iOS project;
Version 1.2.0-pre-1
would it be possible for you to post a simple / sample Xamarin project for this ?
I’m using version 1.2.0-pre3. I’m using it for some Entry and to access camera. All is working fine except the back physical button on Android. As soon as I derive de MainActivity from XFormsApplicationDroid, the back button crash. To further confirm there is a problem on that specific part, I can override OnBackPressed in my MainActivity and not call the base function. The crash is fixed. But I loose the back feature 🙁 Is there something I need to initialize to make the back button work again.
AJ, there should a working sample on the Geolocator ViewModel:
https://github.com/XForms/Xamarin-Forms-Labs/blob/master/samples/Xamarin.Forms.Labs.Sample/ViewModel/GeolocatorViewModel.cs
The problem with null reference could be that iOS linker is not including the assembly. A simple fix in that case would be to do a quick reference to one the classes in the assembly. This would be best done on the AppDelegate. From the sample app:
public override bool FinishedLaunching(UIApplication app, NSDictionary options)
{
this.SetIoc();
new CalendarViewRenderer(); //added so the assembly is included
Hi, when I do step 1, installing from official NuGet, the four packages are installed, but later if you build the project (android) throws an error null refererence
/Library/Frameworks/Mono.framework/External/xbuild/Xamarin/Android/Xamarin.Android.Common.targets: Error: Error executing task LinkAssemblies: error XA2006: Reference to metadata item ‘Android.Views.View/IOnAttachStateChangeListener’ (defined in ‘Xamarin.Forms.Platform.Android, Version=1.2.3.0, Culture=neutral, PublicKeyToken=null’) from ‘Xamarin.Forms.Platform.Android, Version=1.2.3.0, Culture=neutral, PublicKeyToken=null’ could not be resolved. (GUI)
Did I made something wrong? Thank you
For a Windows Phone project the start up page will initialize the Xamarin.Forms framework and then set the startup page by calling