{"id":45216,"date":"2019-09-04T12:33:42","date_gmt":"2019-09-04T19:33:42","guid":{"rendered":"http:\/\/devblogs.microsoft.com\/xamarin\/?p=45216"},"modified":"2020-04-27T14:13:06","modified_gmt":"2020-04-27T21:13:06","slug":"compiled-bindings-xamarin-forms","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/xamarin\/compiled-bindings-xamarin-forms\/","title":{"rendered":"Boost Performance with Compiled Bindings in Xamarin.Forms"},"content":{"rendered":"<p>Data binding is at the core of just about every Xamarin.Forms application. It enabled developers to easily bridge their user interface with their code behind form a simple markup. Data binding also simplifies user interactions and updates to the user interface automatigical! With all this awesome comes with some trade-offs. Xamarin.Forms has to analyze and resolve each binding, which can take precious CPU cycles. This is where compiled bindings come in and help boost performance when using data binding.<\/p>\n<h2 id=\"what-are-compiled-bindings\">What are Compiled Bindings?<\/h2>\n<p>A compiled binding is a way of telling Xamarin.Forms at compile time of the type of data the binding is going to be using. This is means that Xamarin.Forms will not have to use any reflection at runtime to resolve the data binding. By using the special <code>x:DataType<\/code> attribute on any <code>VisualElement<\/code> you can enable a compiled binding. <\/p>\n<h2 id=\"benefits-of-compiled-bindings\">Benefits<\/h2>\n<p>Using these bindings offer a few major benefits for you and your app:<\/p>\n<ol>\n<li>Compile-time validation for all binding expressions.<\/li>\n<li>Performance improvements as bindings are resolved at compile time.<\/li>\n<\/ol>\n<h2 id=\"enabling-xaml-compilation\">Enabling XAML Compilation<\/h2>\n<p>A pre-requisite to use these bindings is that your app must turn on <a href=\"https:\/\/docs.microsoft.com\/xamarin\/xamarin-forms\/xaml\/xamlc?WT.mc_id=compiledbindings-xamarinblog-jamont\">XAML Compilation<\/a>. This is the default in all new apps and greatly improves performance in apps when using XAML. You can enable it by adding an assembly flag in your shared code:<\/p>\n<pre><code>\r\n[assembly:XamlCompilation(XamlCompilationOptions.Compile)]\r\n<\/code><\/pre>\n<h2 id=\"using-compiled-bindings\">Using Compiled Bindings<\/h2>\n<p>Let&#39;s use an example of a page that displays a list of Monkeys in a <code>ListView<\/code>. This means we have a <code>Monkey<\/code> with properties on it about the monkey, a <code>MonkeysViewModel<\/code> that has data objects to bind to for the list, and a <code>MonkeysPage<\/code> that is our user interface in XAML.<\/p>\n<p>Here is what the user interface looks like to display a list of monkeys and the current count:<\/p>\n<pre><code>\r\n&lt;ContentPage \r\n        xmlns=\"http:\/\/xamarin.com\/schemas\/2014\/forms\" \r\n        xmlns:x=\"http:\/\/schemas.microsoft.com\/winfx\/2009\/xaml\" \r\n        x:Class=\"Monkeys.Views.MonkeysPage\"\r\n        Title=\"Monkeys\"&gt;\r\n&lt;StackLayout&gt;\r\n    &lt;Label Margin=\"10,0\" Text=\"{Binding MonkeyCount, StringFormat='{0} Monkeys'}\"\/&gt;\r\n    &lt;ListView ItemsSource=\"{Binding Monkeys}\"\r\n            HasUnevenRows=\"true\"&gt;\r\n        &lt;ListView.ItemTemplate&gt;\r\n            &lt;DataTemplate&gt;\r\n                &lt;ViewCell&gt;\r\n                    &lt;Grid Padding=\"10\" RowSpacing=\"10\" ColumnSpacing=\"10\"&gt;\r\n                        &lt;Grid.ColumnDefinitions&gt;\r\n                            &lt;ColumnDefinition Width=\"Auto\"\/&gt;\r\n                            &lt;ColumnDefinition Width=\"*\"\/&gt;\r\n                        &lt;\/Grid.ColumnDefinitions&gt;\r\n                        &lt;controls:CircleImage \r\n                                BorderColor=\"Aqua\"\r\n                                BorderThickness=\"3\"\r\n                                HeightRequest=\"66\"\r\n                                WidthRequest=\"66\"\r\n                                HorizontalOptions=\"CenterAndExpand\"\r\n                                VerticalOptions=\"CenterAndExpand\"\r\n                                Aspect=\"AspectFill\"\r\n                                Source=\"{Binding Image}\"\/&gt;\r\n                        &lt;StackLayout VerticalOptions=\"Center\"&gt;\r\n                            &lt;Label Text=\"{Binding Name}\"\/&gt;\r\n                            &lt;Label Text=\"{Binding Location}\"\/&gt;\r\n                        &lt;\/StackLayout&gt;\r\n                    &lt;\/Grid&gt;\r\n                &lt;\/ViewCell&gt;\r\n            &lt;\/DataTemplate&gt;\r\n        &lt;\/ListView.ItemTemplate&gt;\r\n    &lt;\/ListView&gt;\r\n&lt;\/ContentPage&gt;\r\n<\/code><\/pre>\n<p>Notice that we are binding from the <code>MonkeysViewModel<\/code> to <code>MonkeyCount<\/code> and <code>Monkeys<\/code> and additionally to the <code>Image<\/code>, <code>Name<\/code>, and <code>Location<\/code> of each monkey inside of the <code>ItemTemplate<\/code>. <\/p>\n<p>To enable these bindings for the <code>MonkeysViewModels<\/code> we will bring in the namespace that it is in:<\/p>\n<pre class=\"lang:xhtml decode:true\">\r\nxmlns:viewmodels=\"clr-namespace:Monkeys.ViewModels\"\r\n<\/pre>\n<p>Then on the <code>ContentPage<\/code> node we can add the <code>x:DataType<\/code> property:<\/p>\n<pre class=\"lang:xhtml decode:true\">\r\n&lt;ContentPage \r\n        xmlns=\"http:\/\/xamarin.com\/schemas\/2014\/forms\" \r\n        xmlns:x=\"http:\/\/schemas.microsoft.com\/winfx\/2009\/xaml\" \r\n        x:Class=\"Monkeys.Views.MonkeysPage\"\r\n        xmlns:viewmodels=\"clr-namespace:Monkeys.ViewModels\"\r\n        x:DataType=\"viewmodels:MonkeysViewModel\"\r\n        Title=\"Monkeys\"&gt;\r\n&lt;\/ContentPage&gt;\r\n<\/pre>\n<p>That is all it takes to start! Now, if for some reason we have a spelling mistake in our bindings when we compile the library we will get an error as the bindings will now be validated.<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/devblogs.microsoft.com\/xamarin\/wp-content\/uploads\/sites\/44\/2019\/08\/Compiled.png\" alt=\"Compiled Binding Error Checks for XAML\" width=\"1215\" height=\"350\" class=\"aligncenter size-full wp-image-45218\" srcset=\"https:\/\/devblogs.microsoft.com\/xamarin\/wp-content\/uploads\/sites\/44\/2019\/08\/Compiled.png 1215w, https:\/\/devblogs.microsoft.com\/xamarin\/wp-content\/uploads\/sites\/44\/2019\/08\/Compiled-300x86.png 300w, https:\/\/devblogs.microsoft.com\/xamarin\/wp-content\/uploads\/sites\/44\/2019\/08\/Compiled-768x221.png 768w, https:\/\/devblogs.microsoft.com\/xamarin\/wp-content\/uploads\/sites\/44\/2019\/08\/Compiled-1024x295.png 1024w\" sizes=\"(max-width: 1215px) 100vw, 1215px\" \/><\/p>\n<p>If you are using any control that has a <code>DataTemplate<\/code> you must also setup the compiled binding on it. In this app, we have a ListView that is data bound to an<code>ObservableCollection&lt;Monkey&gt;<\/code>. We can bring in the <code>Monkey<\/code>&#39;s namespace just like we did for the <code>MonkeysViewModel<\/code>. <\/p>\n<pre class=\"lang:xhtml decode:true\">\r\nxmlns:models=\"clr-namespace:Monkeys.Models\"\r\n<\/pre>\n<p>Then on the <code>DataTemplate<\/code>, we can specify the <code>x:DataType<\/code> like we did before:<\/p>\n<pre class=\"lang:xhtml decode:true\">\r\n&lt;ListView ItemsSource=\"{Binding Monkeys}\"\r\n          HasUnevenRows=\"true\"&gt;\r\n    &lt;ListView.ItemTemplate&gt;\r\n        &lt;DataTemplate x:DataType=\"models:Monkey\"&gt;\r\n            &lt;ViewCell&gt;\r\n            &lt;\/ViewCell&gt;\r\n        &lt;\/DataTemplate&gt;\r\n    &lt;\/ListView.ItemTemplate&gt;\r\n&lt;\/ListView&gt;\r\n<\/pre>\n<p>And just like that we have enabled this on our entire page and it only took a few seconds.<\/p>\n<h2 id=\"learn-more\">Learn More<\/h2>\n<p>There are a lot more ways to use compiled bindings in your Xamarin.Forms apps, so be sure to read through the <a href=\"https:\/\/docs.microsoft.com\/xamarin\/xamarin-forms\/app-fundamentals\/data-binding\/compiled-bindings?WT.mc_id=compiledbindings-xamarinblog-jamont\">comprehensive documentation<\/a>. You can find a sample used in this blog on <a href=\"https:\/\/github.com\/jamesmontemagno\/Xamarin.Forms-Monkeys\">GitHub<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>See how compiled bindings for Xamarin.Forms XAML can help boost performance in your apps and also give immediate feedback when developing apps if you have a typo in your bindings.<\/p>\n","protected":false},"author":544,"featured_media":45218,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[2,367],"tags":[6821,648,5841],"class_list":["post-45216","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-developers","category-xamarin-forms","tag-compiled-bindings","tag-performance","tag-xaml"],"acf":[],"blog_post_summary":"<p>See how compiled bindings for Xamarin.Forms XAML can help boost performance in your apps and also give immediate feedback when developing apps if you have a typo in your bindings.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts\/45216","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\/544"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/comments?post=45216"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts\/45216\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/media\/45218"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/media?parent=45216"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/categories?post=45216"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/tags?post=45216"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}