Xamarin.Forms Embedding – The best of both worlds

Last year at Build 2017, Microsoft announced Xamarin.Forms Embedding. This technology allows you to use Xamarin.Forms inside your Xamarin Native application. Most people will probably choose Xamarin.Forms when they create a new project these days, but for those who are working with Xamarin Native projects targeting multiple OS-es this came as a dearly wanted functionality. As I am daily working on a Xamarin Native project I naturally embraced this technology and advocated for using this in our next big release. I want to share my experiences regarding this and give my thoughts on this approach.

Share ALL the code!

The most obvious advantage of this is the increased code reusage. You can create and define XAML pages which in turn can be initialized on each platform as a UIViewController (Xamarin.iOS) or as a Fragment or Activity (Xamarin.Android). As for our project, we started out with converting one of the static pages in our app (our Help/About page) to be a Xamarin.Forms XAML page, just to test the Embedding technology. And it worked like a charm!

But what if you have a more advanced page with native functionality, like using geolocation, maps or opening a link in a browser?

We solved this in 2 ways: plug-ins and dependency injection. There are lots of plug-ins for Xamarin.Forms for using f.ex. media (camera/video), geolocation, permissions, audio etc. We used these where we could and used dependency injection for the other cases. So for the latter we would pass in an interface to the page, which in iOS would be hooked up to an iOS implementation of that functionality and vice versa for Android.

Designing the pages were done, for the most part, with Xamarin Live Player. We did utilize some of the tricks I mentioned in my previous blog post, more specifically by creating a new project and designing the views there. Our project had too many dependencies to be able to use the Live Player with so this approach worked best for us, although there was a lot of copy-paste action.

Downsides?

I can’t really think of any downsides to this functionality. If you have a problem achieving the same functionality on both platforms with this you can always go back to using native functionality. Another thing to be cautious about is if you have a large Android app and you’re pushing the 64k reference limit. Using this embedding technique means you have to pull in some NuGet packages on both platforms, but I hear the Xamarin team has put a lot of effort into making their packages linker safe, especially with their new all-in-one plugin Xamarin.Essentials.

If you’ve tried out Xamarin.Forms Embedding I’d love to hear your thoughts about it.

 

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.