Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon Can you provide an example of using One-Time binding in MvvmCross


Can you provide an example of using One-Time binding in MvvmCross


Using One-Time Binding in MvvmCross

One-Time binding in MvvmCross is a binding mode where the value is transferred from the ViewModel to the View only once, when the binding is initially set. After this, the binding does not monitor changes and does not perform any updates unless the binding source itself is reset. This mode is useful for fields that are configurable but don't tend to change after they have been initially set, such as static text from language files.

Example of One-Time Binding

To use One-Time binding in MvvmCross, you can follow these steps:

1. Define the ViewModel Property: Ensure that the property in your ViewModel is public and implements `INotifyPropertyChanged` if you want to notify changes, although this is not necessary for One-Time binding.

2. Create the Binding: Use either the fluent binding syntax in code or the XML binding syntax (e.g., in Android AXML files).

Fluent Binding Syntax

Here's how you might create a One-Time binding using fluent bindings in code:

csharp
using MvvmCross.Binding.BindingContext;
using MvvmCross.Binding.Extensions;

public class MyView : MvxViewController
{
    private UILabel _myLabel;

    public override void ViewDidLoad()
    {
        base.ViewDidLoad();

        // Initialize your view components
        _myLabel = new UILabel();

        // Create the binding set
        var set = this.CreateBindingSet();

        // Bind the label text to the ViewModel property using One-Time binding
        set.Bind(_myLabel)
            .For(label => label.Text)
            .To(viewModel => viewModel.StaticText)
            .Mode(MvxBindingMode.OneTime);

        set.Apply();
    }
}

XML Binding Syntax (Android Example)

For Android, you can define bindings directly in the layout XML using the MvxBind attribute. However, specifying the binding mode directly in XML is less common, but you can achieve similar results by defining a custom binding or using fluent bindings.

xml


However, the `Mode=OneTime` part might not work directly in XML without custom setup. It's more common to use fluent bindings for specifying modes.

Important Considerations

- View Recycling: If you're using One-Time binding in a `MvxRecyclerView`, it might not behave as expected due to view recycling. In such cases, the binding will be reapplied every time the view is recycled and appears on the screen again[5].

- Custom Bindings: For more complex scenarios, you might need to create custom bindings to achieve the desired behavior, especially if you need to handle Two-Way or other binding modes on native properties[7].

By using One-Time binding effectively, you can optimize your app's performance by reducing unnecessary updates for static data.

Citations:
[1] https://www.mvvmcross.com/documentation/fundamentals/data-binding
[2] https://www.mvvmcross.com/documentation/advanced/customizing-using-App-and-Setup
[3] https://stackoverflow.com/questions/17156368/
[4] https://blog.ostebaronen.dk/2018/01/mvvmcross-binding-target.html
[5] https://stackoverflow.com/questions/48910503/binding-mode-onetime-in-mvxrecyclerview
[6] https://learn.microsoft.com/en-us/archive/msdn-magazine/2014/august/windows-phone-build-mvvm-apps-with-xamarin-and-mvvmcross
[7] https://stackoverflow.com/questions/41768962/mvvmcross-default-binding-mode-used-in-a-project
[8] https://github.com/MvvmCross/MvvmCross/issues/902