Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon How does MvvmCross handle data binding in AXML for Android


How does MvvmCross handle data binding in AXML for Android


MvvmCross handles data binding in AXML for Android by utilizing a custom attribute called `MvxBind`. This attribute allows developers to bind properties of UI elements in the AXML layout files to properties in the ViewModel. Here's a detailed overview of how it works:

MvxBind Attribute

The `MvxBind` attribute is used in AXML files to specify the binding between a view property and a ViewModel property. For example, if you have a `TextView` and you want to bind its `Text` property to a `Name` property in your ViewModel, you would use the following syntax:

xml

In this example, `Text` is the target property on the `TextView`, and `Name` is the source property in the ViewModel.

Binding Expressions

MvvmCross uses a binding expression syntax similar to Swiss/Tibet, which is specific to MvvmCross. This syntax allows you to specify the target property of the view and the source property of the ViewModel. The general format is `ViewProperty ViewModelProperty`, where `ViewProperty` is the property of the view you want to bind, and `ViewModelProperty` is the property in the ViewModel that you want to bind to.

Two-Way Binding

By default, MvvmCross bindings are two-way, meaning changes to the ViewModel property will update the view, and changes to the view (e.g., user input) will update the ViewModel property. This is different from XAML, where the default binding mode is one-way.

Custom Controls and Attributes

MvvmCross provides custom controls like `MvxListView` and `MvxImageView`, which have additional attributes for binding. For example, you can use `ItemsSource` to bind a collection of items to a list view, and `ItemClick` to bind a command that is executed when an item is clicked:

xml

In this example, `ItemsSource` is bound to the `Trips` collection in the ViewModel, and `ItemClick` is bound to the `SelectTripCommand`.

Fluent Binding

While AXML binding is common for Android, MvvmCross also supports fluent binding, which allows you to define bindings in C# code instead of in the AXML files. This can be useful for more complex bindings or when working with platforms that do not support AXML, like iOS. However, for Android, AXML binding is typically preferred for its clarity and ease of use.

Overall, MvvmCross provides a powerful and flexible data binding system for Android applications, allowing developers to easily connect their UI elements to the underlying ViewModel logic.

Citations:
[1] https://crosscuttingconcerns.com/MvvmCross-Fluent-Databinding
[2] https://leaware.com/creating-cross-platform-xamarin-applications-using-the-mvvmcross-framework/
[3] https://learn.microsoft.com/en-us/archive/msdn-magazine/2014/august/windows-phone-build-mvvm-apps-with-xamarin-and-mvvmcross
[4] https://blog.ostebaronen.dk/2018/01/mvvmcross-binding-target.html
[5] https://www.mvvmcross.com/documentation/getting-started/getting-started
[6] https://stackoverflow.com/questions/28798209/mvvmcross-binding-your-datamodel-to-your-viewmodel
[7] https://stackoverflow.com/questions/46988735/mvvmcross-object-data-binding
[8] https://www.mvvmcross.com/documentation/getting-started/mvvmcross-overview
[9] https://www.mvvmcross.com/documentation/platform/android/android-linearlayout
[10] https://www.mvvmcross.com/documentation/fundamentals/data-binding
[11] https://visualstudiomagazine.com/Articles/2013/02/01/Building-Android-Apps.aspx
[12] https://www.mvvmcross.com/documentation/tutorials/tipcalc/a-xamarinandroid-ui-project