Yes, you can use custom bindings with third-party libraries in MvvmCross. This is particularly useful when the third-party controls do not follow the standard property and event naming conventions that MvvmCross expects for automatic binding. Here's how you can achieve this:
Understanding MvvmCross Bindings
MvvmCross uses a binding framework to connect UI controls with ViewModel properties. It typically works well with controls that have public properties and corresponding change events (e.g., `PropertyChanged` for `INotifyPropertyChanged` implementations). However, when dealing with third-party controls that don't adhere to these conventions, custom bindings are necessary.
Creating Custom Bindings
To create a custom binding for a third-party control, you need to define a `TargetBinding` class. This class specifies how to bind a specific property of the control and, if needed, how to listen for changes to that property.
Here's a step-by-step guide to creating a custom binding:
1. Identify the Property and Event: Determine the property you want to bind on the third-party control and any event that might signal changes to this property.
2. Create a Target Binding Class: Derive your custom binding class from `MvxTargetBinding` or one of its subclasses like `MvxPropertyInfoTargetBinding` or `MvxWithEventPropertyInfoTargetBinding`. These subclasses provide shortcuts for common scenarios.
3. Implement `SetValueImpl`: This method describes how to set the value on the target property.
4. Implement `SubscribeToEvents`: If you need two-way binding, this method is used to subscribe to events that signal changes to the target property.
5. Register the Binding: In your app's `Setup.cs` file, override `FillTargetFactories` to register your custom binding.
Example of a Custom Binding
Let's say you have a third-party control named `MyView` with a property `MyProperty` and an event `MyPropertyChanged`. Here's how you might create a custom two-way binding:
csharp
public class MyViewMyPropertyTargetBinding : MvxPropertyInfoTargetBinding
{
private bool _subscribed;
public override MvxBindingMode DefaultMode => MvxBindingMode.TwoWay;
public MyViewMyPropertyTargetBinding(object target, PropertyInfo targetPropertyInfo)
: base(target, targetPropertyInfo)
{
}
protected override void SetValueImpl(object target, object value)
{
var view = target as MyView;
if (view == null) return;
view.MyProperty = (string)value;
}
public override void SubscribeToEvents()
{
var myView = View;
if (myView == null)
{
MvxBindingTrace.Trace(MvxTraceLevel.Error, "Error - MyView is null in MyViewMyPropertyTargetBinding");
return;
}
_subscribed = true;
myView.MyPropertyChanged += HandleMyPropertyChanged;
}
private void HandleMyPropertyChanged(object sender, EventArgs e)
{
var myView = View;
if (myView == null) return;
FireValueChanged(myView.MyProperty);
}
protected override void Dispose(bool isDisposing)
{
base.Dispose(isDisposing);
if (isDisposing)
{
var myView = View;
if (myView != null && _subscribed)
{
myView.MyPropertyChanged -= HandleMyPropertyChanged;
_subscribed = false;
}
}
}
}
Then, register this binding in your `Setup.cs`:
csharp
protected override void FillTargetFactories(IMvxTargetBindingFactoryRegistry registry)
{
base.FillTargetFactories(registry);
registry.RegisterPropertyInfoBindingFactory(
typeof(MyViewMyPropertyTargetBinding),
typeof(MyView), "MyProperty");
}
Using Converters with Custom Bindings
If the third-party control requires data conversion (e.g., converting between different data types), you can use converters with your custom bindings. MvvmCross provides classes like `MvxConvertingTargetBinding` that support converters, helping prevent feedback loops when updating the target with a new value.
Conclusion
Custom bindings in MvvmCross are powerful tools for integrating third-party controls into your cross-platform applications. By defining how properties are set and how changes are detected, you can seamlessly bind these controls to your ViewModel properties, even when they don't follow standard conventions.
Citations:[1] http://slodge.blogspot.com/2013/06/n28-custom-bindings-n1-days-of-mvvmcross.html
[2] https://www.mvvmcross.com/documentation/getting-started/getting-started
[3] https://stackoverflow.com/questions/20235666/using-non-standard-uis-with-mvvmcross-databindings-for-things-like-gtksharp
[4] https://www.mvvmcross.com/documentation/advanced/custom-data-binding
[5] https://www.mvvmcross.com/documentation/plugins/methodbinding
[6] https://mentormate.com/blog/6-reasons-mvvmcross-speeds-xamarin-development-personalizes-ux/
[7] https://blog.ostebaronen.dk/2018/01/mvvmcross-binding-target.html
[8] https://diglib.tugraz.at/download.php?id=576a78335451d&location=browse