You only need a OneTime binding

When creating a binding in Xamarin.Forms, the default is that you create an OneWay binding (except for input elements that have TwoWay bindings), this means that it updates the view from the ViewModel whenever the value in the ViewModel changes. But often you don't need a OneWay binding, you only need a OneTime binding, but you use OneWay just because it is the default. A OneTime binding only reads the value once, if the property changes after that, the UI is not updated. That means that the UI doesn't need to listen to changes in the ViewModel, and that can affect performance in a good way.

If you are familiar with UWP development and compiled bindings with x:Bind you may know that there is OneTime default and the reason for that is that it gives you a more performant app. So why not use it with Xamarin.Formas as well.

To use OneTime binding one tip from the Jason Smith, one of the founders of Xamarin.Forms, according to this tweet:

In this post, I will show some scenarios where you can use OneTime binding. But I recommend you to use it whenever possible.

When using CollectionView
You maybe will change the data source for a CollectionView (read below for a static data example), so you still need a TwoWay binding there, and often you will set the value of the property after that you have fetched data from a data source. But inside your DataTemplate, you can often use OneTime bindings, and if you have some elements that need to listen for changes, you can use OneTime bindings just for those elements.

When using Commands
When using commands in ViewModels, you often write to them like below, and you never change the value of the property.

In this case, there is no need to use the default OneWay binding because you know that the value of the property will never change.

When having static data
Sometimes you need to bind an element to a collection of static data, for example when you are using a CarouselView or a CollectionView where you specify the data in a ViewModel. The reason that you want to do that is that you can't add items to those elements directly (you can specify ItemSource in XAML if you want).

The property in the ViewModel:

A CarouselView that binds to the property:

5 thoughts on “You only need a OneTime binding

  1. OneTime binding is all the more important in your Command example as the ‘new TinyCommand’ would be executed each time you query the property. I’d have rather written ‘{ get; } =’ instead of ‘=>’

  2. Rob Wilson says:

    I think this paragraph is confusing…

    “But often you don’t need a OneWay binding, you only need a OneTime binding, but you use OneWay just because it is the default”

    Plus you start with “When creating a binding in Xamarin.Forms, the default is that you create an OneWay binding” but I don’t specify one-way or two-way so isn’t the default two-way as implied by your later mention of UWP defaulting to one-way whereas XF does not?

    I hadn’t considered 1 v 2 way for performance and I certainly like the idea of leaving the carousel view 2-way but the template 1 way. It would be good if the Intellisense could detect how you use the commands and properties and suggest one-way if it’s only read once, or go into a special debug mode that watches/counts and advises in a report where one-time binding would be beneficial.

    • dhindrik says:

      I will take a look at the paragraph.

      TwoWay bindings are the default for input controls, but for other controls, OneWay is default.

Leave a Reply to odalet Cancel 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.