MvvmCross provides a flexible data binding system that allows you to link views with their view models. While MvvmCross itself does not explicitly list custom XML attributes for data binding in its documentation, it does support using XML attributes like `MvxBind` in AXML files for Android. This attribute is used to specify the property of the control to bind and the property of the ViewModel to use as the source.
MvxBind Attribute
The `MvxBind` attribute is the primary XML attribute used for data binding in MvvmCross on Android. It is applied to views in the AXML layout files to bind properties of the view to properties of the ViewModel. Here's an example of how it's used:
xml
In this example, the `Text` property of the `TextView` is bound to the `Name` property of the ViewModel.
Custom Bindings
For properties that do not have a direct setter or getter (like `TextColor` in Mono for Android), you need to create a custom binding. This involves creating a `TargetBinding` class that defines how to set the value and optionally listen for changes.
For instance, to bind a data object property to a method like `SetTextColor()`, you would create a custom target binding similar to this:
csharp
public class TextViewTextColorTargetBinding : MvxConvertingTargetBinding
{
public TextViewTextColorTargetBinding(TextView target)
: base(target)
{
}
protected override void SetValueImpl(object target, object value)
{
var view = target as TextView;
if (view != null)
{
view.SetTextColor((Color)value);
}
}
public override Type TargetType => typeof(TextView);
public override MvxBindingMode DefaultMode => MvxBindingMode.OneWay;
}
Then, you need to register this binding in your `Setup.cs` file:
csharp
protected override void FillTargetFactories(IMvxTargetBindingFactoryRegistry registry)
{
base.FillTargetFactories(registry);
registry.RegisterCustomBindingFactory(
"TextColor", view => new TextViewTextColorTargetBinding(view));
}
After registering the binding, you can use it in your AXML layout like so:
xml
MvxBindingAttributes.xml
MvvmCross also uses an `MvxBindingAttributes.xml` file to define some custom attributes that can be used in data binding. However, this file is more about defining the XML namespace and attributes for MvvmCross bindings rather than providing additional custom attributes for data binding.
In summary, while MvvmCross does not provide a wide range of custom XML attributes for data binding, it offers a flexible system to create custom bindings using C# code. The `MvxBind` attribute is the primary way to bind properties in AXML files for Android, and custom bindings can be created for properties that do not have direct setters or getters.
Citations:[1] https://www.mvvmcross.com/documentation/advanced/custom-data-binding
[2] https://stackoverflow.com/questions/12596553/attributes-from-mvxbindingattributes-are-not-added-with-mvvmcross
[3] http://stackoverflow.com/questions/10700445/in-mvvmcross-how-do-i-do-custom.-bind-properties
[4] https://www.mvvmcross.com/documentation/fundamentals/data-binding
[5] https://stackoverflow.com/questions/51312982/mvvmcross-pass-data-from-xml-to-android/51328557
[6] https://github.com/MvvmCross/MvvmCross/blob/develop/CHANGELOG.md
[7] https://learn.microsoft.com/en-us/archive/msdn-magazine/2014/august/windows-phone-build-mvvm-apps-with-xamarin-and-mvvmcross
[8] https://github.com/MvvmCross/MvvmCross/issues/87
[9] https://stackoverflow.com/questions/10700445/in-mvvmcross-how-do-i-do-custom-bind-properties