{"id":27849,"date":"2016-09-29T14:13:19","date_gmt":"2016-09-29T21:13:19","guid":{"rendered":"https:\/\/blog.xamarin.com\/?p=27849"},"modified":"2016-09-29T14:13:19","modified_gmt":"2016-09-29T21:13:19","slug":"adding-bindable-native-views-directly-to-xaml","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/xamarin\/adding-bindable-native-views-directly-to-xaml\/","title":{"rendered":"Adding Bindable Native Views Directly to XAML"},"content":{"rendered":"<p>\t\t\t\tXamarin.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 <a href=\"https:\/\/developer.xamarin.com\/guides\/xamarin-forms\/dependency-service\/\"><code>DependencyService<\/code><\/a>. For adding platform-specific controls such as the <code>FloatingActionButton<\/code> on Android, we have many different options at our disposal, from <a href=\"https:\/\/developer.xamarin.com\/guides\/xamarin-forms\/custom-renderer\/\">custom renderers<\/a> to <a href=\"https:\/\/developer.xamarin.com\/guides\/xamarin-forms\/effects\/\">effects<\/a> to <a href=\"https:\/\/developer.xamarin.com\/guides\/xamarin-forms\/user-interface\/layouts\/add-platform-controls\/\">native embedding<\/a>.<\/p>\n<p>To help you easily build beautiful user interfaces with platform-specific controls, we&#8217;re proud to introduce <strong>native view declaration<\/strong>, 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 <em>no<\/em> additional configuration. In this blog post, you&#8217;ll learn how to add bindable iOS, Android, and Windows views directly to Xamarin.Forms XAML with native view declaration.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/44\/2019\/03\/68747470733a2f2f646c2e64726f70626f7875736572636f6e74656e742e636f6d2f752f313936363536392f6f7574322e676966.gif\" alt=\"An example of using native view declaration with two-way data binding to create a color picker.\" width=\"600\" height=\"300\" class=\"aligncenter size-full wp-image-27906\" \/><\/p>\n<h2>Introducing Native View Declaration<\/h2>\n<h3>Getting Started<\/h3>\n<p>Native view declaration requires that you have the <a href=\"https:\/\/developer.xamarin.com\/recipes\/cross-platform\/ide\/change_updates_channel\/\">latest Stable channel release<\/a>, <a href=\"https:\/\/releases.xamarin.com\/stable-release-cycle-8-service-release-0\/\">Service Release 0<\/a>, which also allows you to take advantage of the new iOS 10 and Android Nougat APIs in your mobile apps. <a href=\"https:\/\/www.nuget.org\/packages\/Xamarin.Forms\/2.3.3.152-pre2\">Xamarin.Forms 2.3.3-pre<\/a> 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&#8217;t update any of the <em>Xamarin.Android.Support<\/em> packages; Xamarin.Forms will automatically update these packages if a newer compatible version is available.<\/p>\n<h3>Adding Native Views to XAML<\/h3>\n<p>Adding native views couldn&#8217;t be easier! To make native views consumable via XAML, we must first add XML namespaces (markup <code>xmlns<\/code>) for each platform we&#8217;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:<\/p>\n<pre class=\"theme:vs2012 lang:csharp decode:true\">\n\n\n\t\n\t    \n        \n        \n\t\n\n<\/pre>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/44\/2019\/03\/Artboard.png\" alt=\"Native View Declaration\" width=\"490\" height=\"395\" class=\"aligncenter size-full wp-image-27912\" \/><\/p>\n<p>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 <code>Arguments XML<\/code> attribute. <\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/44\/2019\/03\/2016-09-29_1446.png\" alt=\"IntelliSense for Xamarin.Forms XAML with native view declaration.\" width=\"625\" height=\"342\" class=\"aligncenter size-full wp-image-27909\" \/><\/p>\n<h2>Data Binding Native Views<\/h2>\n<p>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.<\/p>\n<p>Native view declaration not only supports <code>OneWay<\/code> data bindings, where changes are propagated from source to target object, but also <code>TwoWay<\/code> 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.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/44\/2019\/03\/68747470733a2f2f646c2e64726f70626f7875736572636f6e74656e742e636f6d2f752f313936363536392f6f7574322e676966.gif\" alt=\"An example of using native view declaration with two-way data binding to create a color picker.\" width=\"800\" height=\"600\" class=\"aligncenter size-full wp-image-27906\" \/><\/p>\n<p>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.<\/p>\n<h2>Built on Open Source<\/h2>\n<p>In addition to the Xamarin SDKs being open-sourced on GitHub, <a href=\"http:\/\/open.xamarin.com\">Xamarin.Forms is also open source<\/a>! 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&mdash;<a href=\"http:\/\/open.xamarin.com\/contribute\">there are many ways you can contribute<\/a>. We would love to <a href=\"https:\/\/forums.xamarin.com\/77854\/xamarin-forms-2-3-3-pre2\/p1\">hear your feedback<\/a> on native view declaration and bindings on the Xamarin Forums and <a href=\"https:\/\/bugzilla.xamarin.com\/newbug\">Bugzilla<\/a> if you experience issues.<\/p>\n<h2>Wrapping Up<\/h2>\n<p>In this blog post, we learned how to use the new <strong>native view declaration<\/strong> and <strong>native bindings<\/strong> 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, <a href=\"http:\/\/forums.xamarin.com\/discussion\/77854\/xamarin-forms-2-3-3-pre2\/p1\">visit the Xamarin Forums<\/a>. For samples of native view declaration, <a href=\"https:\/\/github.com\/rmarinho\/XFNativeMix\">visit GitHub<\/a>.\t\t<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 [&hellip;]<\/p>\n","protected":false},"author":546,"featured_media":27906,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[2],"tags":[16],"class_list":["post-27849","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-developers","tag-xamarin-forms"],"acf":[],"blog_post_summary":"<p>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 [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts\/27849","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/users\/546"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/comments?post=27849"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts\/27849\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/media?parent=27849"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/categories?post=27849"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/tags?post=27849"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}