4 Tips To Build Production Apps Easier With Xamarin Forms
April 23, 2015
*Bluetube was acquired in October 2016. To learn more please click here.*
4 Tips To Build Production Apps Easier With Xamarin.Forms
Xamarin.Forms is a game changer in mobile development. Introduced in May 2014, Xamarin.Forms provides developers with a way to share user interface code. This ability to share such an impressive amount of code greatly cuts down on development time. The initial releases of Xamarin.Forms were excellent for making quick prototypes and internal applications that didn’t require complex user interfaces. Over the past 11 months, the Forms team has fixed bugs and added improvements to the API making the most recent release (v1.4) feel like a very polished product. It seems that Xamarin.Forms has finally matured enough to where it can be used to build a true production application without making compromises in user interface design. By leveraging the power of custom renderers, developers can use Xamarin.Forms to create production applications that are fully featured and boast an impressive user interface.
Here are four useful tips to keep in mind when creating custom renderers to help you build your next mobile app in Xamarin.Forms:
1. Customizing Built-in Controls or Creating New Ones
Built-in Xamarin.Forms controls have existing custom renderers. When you need to customize a built-in control, you can inherit from the existing custom renderer and add customizations. Xamarin's documentation on creating custom renderers is a great place to get started: http://developer.xamarin.com/guides/cross-platform/xamarin-forms/custom-renderer/
When making a brand new control, create a custom renderer that inherits from ViewRenderer<TView, TNativeView>, where TView is the type that represents the new Xamarin.Forms View and TNativeView is the type of native control that this custom control maps to. Check out the following blog post to see it in action: http://blog.xamarin.com/using-custom-controls-in-xamarin.forms-on-android/
2. Building Custom Page Renders
What if you need to customize the way a page is rendered? You guessed it; there are renderers for that too! If the goal is to add some functionality to an existing page type, just inherit from the associated renderer for that page type and customize! One of the challenges in writing custom renderers is determining the correct base class. Here is a fantastic reference guide that helps determine appropriate base classes: http://magenic.com/Blog/PostId/31/xamarin-xamarinforms-renderer-reference
3. Rendering Pages Differently
What if you want a page to be rendered differently from what Xamarin.Forms provides? For example, a TabbedPage is rendered on Android as Tabs underneath the Action Bar, with automatic horizontal scrolling when there are too many tabs to display all at once. On iOS however, the TabbedPage is rendered as a UITabbarController, with a bottom Tab Bar. When there are too many tabs to display, the iOS renderer creates a "More..." tab that navigates the user to a table of additional options.
Android Tab Bar
iOS Tab Bar
What if the goal is to make the iOS experience more aligned with the Android experience? In cases such as these, a completely custom page can be created. Simply create a class that derives from PageRenderer. Check out this blog post for a great walkthrough of using PageRenderer to create a custom UIViewController: http://blog.xamarin.com/using-custom-uiviewcontrollers-in-xamarin.forms-on-ios/
4. Using The RenderFactory Class
While writing a custom page renderer for iOS, you may find that you need a way to access the native UIView representation of a Xamarin.Forms StackLayout. In cases like these, the RendererFactory class in Xamarin.Forms exposes a static method GetRenderer(...) that takes the Xamarin.Forms control as a parameter and returns the associated renderer for it. With the renderer, you can easily access the native representation of the control by simply using the NativeView public property.
Is Xamarin.Forms Really Ready for App Production?
Absolutely. For many enterprise companies aiming to expand its mobile presence, Xamarin.Forms is the ideal solution. Even though writing custom renderers requires platform specific UI code, standard pages like login screens and fly-out navigation menus can be written entirely with Xamarin.Forms resulting in an overall increase in shared code. Meaning, developers can build easier, faster, and more securely for native cross-platform use. The build lifecycle is shortened tremendously and the time to market is much more rapid.
If you would like someone to assess your mobile application strategy with Xamarin.Forms, feel free to reach out and contact us. We will be happy to get you on the right track with Xamarin.Forms.