Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon What are the common use cases for Fluent Databinding in MvvmCross


What are the common use cases for Fluent Databinding in MvvmCross


Fluent databinding in MvvmCross is a powerful tool for creating strong, type-safe bindings between views and view models. It is particularly useful in platforms like iOS and macOS, where XML layout files are not used, and bindings must be set in code behind. Here are some common use cases for Fluent databinding in MvvmCross:

Binding Views to ViewModels

Fluent databinding allows developers to explicitly define how properties of a view should be bound to properties of a view model. This is done by creating a binding set using `CreateBindingSet`, specifying the target view property and the source view model property. For example:

csharp
var set = this.CreateBindingSet();
set.Bind(someView)
   .For(view => view.ViewProperty)
   .To(viewModel => viewModel.ViewModelProperty);
set.Apply();

This approach ensures that changes in the view model are reflected in the view and vice versa, depending on the binding mode (OneWay, TwoWay, etc.).

Handling Complex Bindings

Fluent databinding is especially useful when dealing with complex bindings that require value converters or specific binding modes. For instance, you can use value converters to transform data between the view and view model. Here's an example using a value converter:

csharp
set.Bind(field)
   .For(l => l.Value)
   .To(vm => vm.CurrentValue)
   .WithConversion("Power", 2);

This example applies a "Power" converter with a parameter of 2 to the binding.

Custom Data Binding

In cases where MvvmCross does not support a specific view or control out of the box, Fluent databinding can be used to create custom bindings. Developers can define their own target bindings by inheriting from `MvxConvertingTargetBinding` or using simpler classes like `MvxPropertyInfoTargetBinding` for one-way bindings. This allows for full control over how data is exchanged between the view and view model.

Platform-Specific Bindings

On platforms like Android, where XML layout files are used, Fluent databinding can still be beneficial for dynamic or complex bindings that are easier to manage in code. For iOS and macOS, where XML layouts are not used, Fluent databinding is essential for setting up bindings.

Event Handling

Fluent databinding can also be used to bind events, such as button clicks, to commands in the view model. This is particularly useful for handling user interactions in a decoupled manner.

Overall, Fluent databinding in MvvmCross provides a flexible and powerful way to manage data bindings across different platforms, making it easier to develop cross-platform applications with a robust MVVM architecture.

Citations:
[1] https://yanxiaodi.gitbook.io/xamarin-mvvmcross-handbook/using-mvvmcross-to-create-your-first-xamarin-app/creating-the-ios-project/understanding-the-data-binding-syntax
[2] https://www.mvvmcross.com/documentation/fundamentals/value-converters
[3] https://www.mvvmcross.com/documentation/advanced/custom-data-binding
[4] https://www.mvvmcross.com/mvvmcross-5-release/
[5] https://blog.ostebaronen.dk/2018/01/mvvmcross-binding-target.html
[6] https://stackoverflow.com/questions/39961865/how-to-use-the-mvvmcross-fluent-api-to-bind-a-recyclerview-items-textview-to-a
[7] https://stackoverflow.com/questions/28798209/mvvmcross-binding-your-datamodel-to-your-viewmodel
[8] https://github.com/MvvmCross/MvvmCross/blob/develop/CHANGELOG.md
[9] https://stackoverflow.com/questions/60311571/the-intended-purpose-of-mvvmcross-createbindingset
[10] https://www.mvvmcross.com/documentation/fundamentals/data-binding
[11] https://crosscuttingconcerns.com/MvvmCross-Fluent-Databinding
[12] https://learn.microsoft.com/en-us/archive/msdn-magazine/2014/august/windows-phone-build-mvvm-apps-with-xamarin-and-mvvmcross