Adding Bindable Native Views Directly to XAML

Pierce Boggan

Xamarin.Forms ships with over 40 pages, layouts, and controls for you to mix-and-match to build great native cross-platform user interfaces. One of my favorite features of Xamarin.Forms is that we have access to 100% of the controls and APIs for iOS, Android, and Windows. For implementing platform-specific features such as geolocation or bluetooth, we can take advantage of the DependencyService. For adding platform-specific controls such as the FloatingActionButton on Android, we have many different options at our disposal, from custom renderers to effects to native embedding.

To help you easily build beautiful user interfaces with platform-specific controls, we’re proud to introduce native view declaration, allowing you to add bindable iOS, Android, and Windows views directly to XAML. Rather than having to write a custom renderer, you can simply add the control directly to XAML with no additional configuration. In this blog post, you’ll learn how to add bindable iOS, Android, and Windows views directly to Xamarin.Forms XAML with native view declaration.

An example of using native view declaration with two-way data binding to create a color picker.

Introducing Native View Declaration

Getting Started

Native view declaration requires that you have the latest Stable channel release, Service Release 0, which also allows you to take advantage of the new iOS 10 and Android Nougat APIs in your mobile apps. Xamarin.Forms 2.3.3-pre introduced support for native view declaration and bindings, so you must be using at least that version of Xamarin.Forms to use this feature. As with all versions of Xamarin.Forms, don’t update any of the Xamarin.Android.Support packages; Xamarin.Forms will automatically update these packages if a newer compatible version is available.

Adding Native Views to XAML

Adding native views couldn’t be easier! To make native views consumable via XAML, we must first add XML namespaces (markup xmlns) for each platform we’ll be embedding views from. This helps Xamarin.Forms find the native control added to XAML. Controls defined for XAML namespaces other than the specified target platform are ignored. The target platform is automatically selected depending on the platform the app is running on. Next, we can add native view directly to our XAML:



	
	    
        
        
	

Native View Declaration

We can access all native properties for the control directly in our XAML as XML attributes, as well as view these properties via XAML IntelliSense. If the native control requires use of arguments, we can pass these to the native control using the Arguments XML attribute.

IntelliSense for Xamarin.Forms XAML with native view declaration.

Data Binding Native Views

The ability to add native views directly to XAML is great, but many controls also require user interaction, such as entering text. Data bindings allow properties of two objects to be linked so that a change in one causes a change in the other. Native view declaration supports data binding out of the box, so you can bind to properties of native views from within XAML.

Native view declaration not only supports OneWay data bindings, where changes are propagated from source to target object, but also TwoWay data bindings. Two-way data binding propagates changes in both directions, allowing us to ensure that two views are always synchronized. This allows us to build very complex views, such as this color picker, with native view declaration in Xamarin.Forms.

An example of using native view declaration with two-way data binding to create a color picker.

Unlike native embedding, native view declaration works in Portable Class Libraries (PCLs) as well. Native data binding will also work in PCLs, though you will need to ensure you have the XAML compiler (XAMLC) turned off for pages that use native view declaration.

Built on Open Source

In addition to the Xamarin SDKs being open-sourced on GitHub, Xamarin.Forms is also open source! We want it to be as easy as possible for developers to contribute to Xamarin.Forms. Being part of an open source project means much more than just writing code—there are many ways you can contribute. We would love to hear your feedback on native view declaration and bindings on the Xamarin Forums and Bugzilla if you experience issues.

Wrapping Up

In this blog post, we learned how to use the new native view declaration and native bindings features in Xamarin.Forms 2.3.3 and above to add platform-specific controls directly to XAML without requiring a custom renderer. For more information about native view declaration, visit the Xamarin Forums. For samples of native view declaration, visit GitHub.

0 comments

Discussion is closed.

Feedback usabilla icon