Creating a custom `TargetBinding` in MvvmCross allows you to bind properties of views that are not supported out of the box or require additional handling, such as two-way bindings. Here's a step-by-step guide on how to create a custom `TargetBinding` for a hypothetical view called `MyView` with a property `MyProperty`.
Step 1: Define the Custom Target Binding Class
First, you need to create a class that inherits from `MvxPropertyInfoTargetBinding` or another appropriate base class depending on your needs. For a simple property binding, `MvxPropertyInfoTargetBinding` is suitable.
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;
}
}
}
}
Step 2: Register the Custom Target Binding
After defining the custom `TargetBinding`, you need to register it in your `Setup.cs` file. This is done by overriding the `FillTargetFactories` method:
csharp
protected override void FillTargetFactories(IMvxTargetBindingFactoryRegistry registry)
{
base.FillTargetFactories(registry);
registry.RegisterPropertyInfoBindingFactory(
typeof(MyViewMyPropertyTargetBinding),
typeof(MyView), "MyProperty");
}
Step 3: Use the Custom Target Binding in Your View
Now that the custom binding is registered, you can use it in your views like any other binding:
csharp
MyView aView;
set.Bind(aView).For("MyProperty").To(vm => vm.value);
Explanation
- TargetBinding Class: The `MyViewMyPropertyTargetBinding` class defines how to bind `MyProperty` of `MyView`. It handles setting the property value and subscribing to the `MyPropertyChanged` event for two-way binding.
- Registration: The binding is registered in `Setup.cs` to make it available for use.
- Usage: The custom binding is used in views by specifying the property name (`MyProperty`) and the source property (`vm.value`) in the view model.
This example demonstrates how to create a custom `TargetBinding` for a specific property, enabling two-way data binding in MvvmCross.
Citations:[1] https://www.mvvmcross.com/documentation/advanced/custom-data-binding
[2] https://stackoverflow.com/questions/37876554/mvvmcross-how-to-bind-xamarin-android-events-to-viewmodel-commands
[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://livebook.manning.com/book/xamarin-in-action/b-using-mvvm-light-instead-of-mvvmcross/v-15/
[7] https://leaware.com/creating-cross-platform-xamarin-applications-using-the-mvvmcross-framework/
[8] https://github.com/MvvmCross/MvvmCross/issues/902