Pallet Town: Xamarin.Forms Basic IOC

Welcome to Pallet Town! Pallet town is going to be an ongoing bi-weekly look at various development technologies and techniques from the perspective of someone new to that technology. For those who might know or (God forbid) are too young to get the reference, Pallet Town is the first town in the original Pokemon games for the Nintendo Gameboy.

Xamarin.Forms is a cross platform tool that allow code re-use for iOS and Android. It works by interpreting your C# code and layout it out using what are usually reasonable conventions for the specific platform’s UX paradigms. For example, a Forms Tabbar lays out on the bottom of the screen in iOS but the top on Android devices, thus following the conventions for both platforms correctly.

Forms makes good use of the common .Net inversion of control development technique. If you haven’t worked in the .Net space, then IOC may seem a bit strange – indeed, it has taken me some time to actually figure out how it works but it’s actually pretty simple to get started with and is extremely powerful in terms of code re-use and modular design. Forms.Labs, an open-source collection of classes designed to jump start your Forms development, uses IOC to great effect to help you easily access device specific functionality from your shared Forms code, rather than having to write platform specific code.

Let’s take a simple case of wanting to import an photo from your device and wanting to keep it as much as possible on the Forms level, rather than dropping to platform specific code. Here’s the shared code that would likely be on a Page in your app:

IMediaPicker picker = DependencyService.Get<IMediaPicker> ();

MediaFile img = await picker.SelectPhotoAsync (new CameraMediaStorageOptions {

MaxPixelDimension = 100 // arbitrary size

});



OK, so you’ve picked an image. Now let’s take things a step further and say that you want to save it to disk for some other use, such as uploading it to S3 or maybe just to have for later:

var fileAccess = Resolver.Resolve<IFileAccess> ();

string imageName = "somename" + "jpeg"; // assuming jpeg

fileAccess.WriteStream (imageName, img.Source);



What we are doing is uses a shared / abstract interface to call onto both the phones image gallery in the first snippet and the filesystem in the second. So far so simple and I’m happy to say it really never gets much more complex. We just have to do a little platform specific code. First, let’s take a look at our AppDelegate for iOS:

var resolverContainer = new SimpleContainer ();

resolverContainer.Register<IDevice> (t => AppleDevice.CurrentDevice)

.Register<IDependencyContainer> (t => resolverContainer)

.Register<IFileAccess> (t =>; new FileAccess ());

Resolver.SetResolver (resolverContainer.GetResolver ());



You’ll either want to add this code to your ‘FinishedLaunching’ method or create a separate method and call that method from ‘FinishedLaunching’. Now, let’s take a look at Android where we will be working in our MainActivity:

var resolverContainer = new SimpleContainer ();

resolverContainer.Register<IDevice> (t => AndroidDevice.CurrentDevice)

.Register<IDependencyContainer> (t => resolverContainer)

.Register<IFileAccess> (t => new FileAccess ());

Resolver.SetResolver (resolverContainer.GetResolver ());



Notice that the code is just about identical with the obvious ‘AppleDevice’ versus ‘AndroidDevice’ difference. Basically, what we are doing is setting the correct platform specific objects upfront on application launch, so that when we call the first snippets in our shared code, we don’t have to care which platform the code is actually running on.

Developers experienced with IOC will note that this is a pretty simple example of IOC and it is capable of much more than just abstracting away the device platform. If you’re just getting started on your development journey and are having a hard time wrapping your head around the snippets here, don’t worry about it and come back to it later. This is just mean to be an introduction to IOC on Xamarin.

Hope this post helps you on your journey to become a Pokemon… err coding master! Please feel free to send any feedback to me on Twitter or Google+. Also, be sure to check out my company Fingertip Tech, INC on Twitter.