This project has moved. For the latest updates, please go here.
1
Vote

Binding to Interfaces fail

description

I'm trying to bind an ICommand from view to ViewModel in an Android/iOS native app. Unfortunately this fails because of the SimpleConverter which tries to convert using System.Convert.ChangeType(value, typeof (TTarget));

This method creates a new object and obviously interfaces cannot be instanciated. A check should be added to validate if a direct cast can be done before attempting to use System.Convert. The following fails but if you change ICommand to RelayCommand on both View and VM, it works. Using an empty converter also works (3rd code block).

View
        public ICommand UpdateWatermarkCommand { get; }

        public override void ViewDidLoad()
        {
            base.ViewDidLoad();

            this.SetBinding(() => UpdateWatermarkCommand, ViewModel, () => ViewModel.DisplayWatermarkCommand, BindingMode.OneTime);
        }
ViewModel
        public ICommand DisplayWatermarkCommand { get; set; }
Using empty converter
        public override void ViewDidLoad()
        {
            base.ViewDidLoad();

            this.SetBinding(() => UpdateWatermarkCommand, ViewModel, () => ViewModel.DisplayWatermarkCommand, BindingMode.OneTime).ConvertSourceToTarget(DirectMap);
        }

        private ICommand DirectMap(ICommand arg)
        {
            return arg;
        }

comments

lbugnion wrote Aug 19 at 1:38 AM

Hi,

Interesting use case. Out of curiosity, why do you want to bind commands together?

That said I see your point about interfaces. I will add an item to my backlog,

Thanks
Laurent

lerondpoint wrote Aug 19 at 2:15 AM

Yes it is a pretty theoretical use of bindings and of course in the usual lifetime of the view & VM it is equivalent to a simple assigment. However, I chose to do this in case the View Model changes at runtime which I'm not even sure is properly handled by your binding engine to be honest (given the binding is done on the object directly as opposed to using the dot operator in Xaml)

With that said, it is common to bind commands through Xaml in Xamarin Forms, Wpf or UWP and in the best world your binding engine would allow it to.

lbugnion wrote Aug 19 at 2:44 AM

Yes in XAML they use a Binding to add a Command to a UI element which supports it. There is no such thing in Android or iOS since the UI elements don't have a Command property, but I propose to use the SetCommand extension method to achieve the same effect.

For example

_myButton.SetCommand(AnyCommand); // which uses the default event for the UI element, for example Click for a button

or
_myButton.SetCommand("EventName", AnyCommand); // if you want to observe another event.

Would that make more sense?
Laurent

lerondpoint wrote Aug 19 at 3:03 AM

Not sure how I missed the SetCommand method but I wrote my own weak subscription and binding classes to map events to commands.

lerondpoint wrote Aug 19 at 3:04 AM

Not sure how I missed the SetCommand methods but I wrote my own weak subscription and binding classes to bind events to commands.