简体   繁体   中英

How to use custom delegate and MvvmCross Binding on Xamarin.iOS?

I have custom UITextView:

public partial class EditableDescriptionTextView : UITextView, IUITextViewDelegate 
{
    ctors()...//Invokes Initialize();
    void Initialize()
    {
       Delegate = this;
    }

    //Override methods I need to handle
}

In my View I have a binding to the ViewModel like so:

bindingSet.Bind(EditableDescriptionTextView)
    .To(vm => vm.Description);

The problem is that firstly works that string: Delegate = this . It Ok, but when works bindingSet.Bind(...).To(...) seems that delegate changes and application crashes with exception like :

Event registration is overwriting existing delegate. Either just use events or your own delegate

The same error happens when I directly try to assign delegate after bindings like:

// CustomDelegate extends UIViewTextDelegate
EditableDescriptionTextView.Delegate = new CustomDelegate();

In other words, app fails each time when I reassign the delegate. Is there any possibility to use my own delegate with MvvmCross or what is the best workaround of this situation?

PS: Originally I need to implement functional like here ShouldChangeTextInRange is not called for UITextView , but I also need mvvxcross binding.

This doesn't seem like a MvvmCross problem. MvvmCross does not attach any delegates to UITextFields. But since you are implementing your own Delegate, eventhandlers that MvvmCross uses to listen to changes in such views for TwoWay bindings, will probably not work after you have attached your custom delegate.

So I would simply stick to the events that UITextField provides you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM