Mvvmcross: UITextView target binding fails when subscribing to changes

Created on 20 Dec 2017  路  6Comments  路  Source: MvvmCross/MvvmCross



Steps to reproduce :scroll:

  1. Create a view with a UITextView on it.

  2. Create a binding for the Text property on the UITextView 馃敆

```C#
var set = this.CreateBindingSet();
set.Bind(CommentTextField).For(t => t.Text).To(vm => vm.Comment);
set.Apply();


3. Exception will happen as soon as Apply() is being called 馃樀

**Stack trace:** 馃攷

2017-12-20 15:40:48.421 App.iOS[7032:2096174] Unhandled managed exception:
Object of type 'System.EventHandler' cannot be converted to type 'System.EventHandler1[UIKit.NSTextStorageEventArgs]'. (System.ArgumentException) at System.RuntimeType.CheckValue (System.Object value, System.Reflection.Binder binder, System.Globalization.CultureInfo culture, System.Reflection.BindingFlags invokeAttr) [0x00056] in /Library/Frameworks/Xamarin.iOS.framework/Versions/11.6.1.2/src/mono/mcs/class/corlib/ReferenceSources/RuntimeType.cs:195 at System.Reflection.MonoMethod.ConvertValues (System.Reflection.Binder binder, System.Object[] args, System.Reflection.ParameterInfo[] pinfo, System.Globalization.CultureInfo culture, System.Reflection.BindingFlags invokeAttr) [0x00069] in /Library/Frameworks/Xamarin.iOS.framework/Versions/11.6.1.2/src/mono/mcs/class/corlib/System.Reflection/MonoMethod.cs:345 at System.Reflection.MonoMethod.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00011] in /Library/Frameworks/Xamarin.iOS.framework/Versions/11.6.1.2/src/mono/mcs/class/corlib/System.Reflection/MonoMethod.cs:293 at System.Reflection.MethodBase.Invoke (System.Object obj, System.Object[] parameters) [0x00000] in /Library/Frameworks/Xamarin.iOS.framework/Versions/11.6.1.2/src/mono/mcs/class/referencesource/mscorlib/system/reflection/methodbase.cs:229 at MvvmCross.Platform.WeakSubscription.MvxWeakEventSubscription1[TSource].AddEventHandler () [0x0002e] in <982b100fcc964a2485777ac711b6e1d9>:0
at MvvmCross.Platform.WeakSubscription.MvxWeakEventSubscription1[TSource]..ctor (UIKit.NSTextStorage source, System.Reflection.EventInfo sourceEventInfo, System.EventHandler targetEventHandler) [0x00063] in <982b100fcc964a2485777ac711b6e1d9>:0 at MvvmCross.Platform.WeakSubscription.MvxWeakEventSubscription1[TSource]..ctor (UIKit.NSTextStorage source, System.String sourceEventName, System.EventHandler targetEventHandler) [0x00012] in <982b100fcc964a2485777ac711b6e1d9>:0
at MvvmCross.Platform.WeakSubscription.MvxWeakSubscriptionExtensionMethods.WeakSubscribe[TSource] (TSource source, System.String eventName, System.EventHandler eventHandler) [0x00000] in <982b100fcc964a2485777ac711b6e1d9>:0
at MvvmCross.Binding.iOS.Target.MvxUITextViewTextTargetBinding.SubscribeToEvents () [0x00053] in <7d61c8f5ab854be8acc6bbbd812e10fc>:0
at MvvmCross.Binding.Bindings.MvxFullBinding.CreateTargetBinding (System.Object target) [0x00057] in :0
at MvvmCross.Binding.Bindings.MvxFullBinding..ctor (MvvmCross.Binding.MvxBindingRequest bindingRequest) [0x0002f] in :0
at MvvmCross.Binding.Binders.MvxFromTextBinder.BindSingle (MvvmCross.Binding.MvxBindingRequest bindingRequest) [0x00000] in :0
at MvvmCross.Binding.Binders.MvxFromTextBinder+<>c__DisplayClass2_0.b__0 (MvvmCross.Binding.Bindings.MvxBindingDescription description) [0x00018] in :0
at System.Linq.Enumerable+SelectArrayIterator2[TSource,TResult].MoveNext () [0x0003a] in /Library/Frameworks/Xamarin.iOS.framework/Versions/11.6.1.2/src/mono/external/corefx/src/System.Linq/src/System/Linq/Select.cs:233 at MvvmCross.Binding.BindingContext.MvxBindingContextOwnerExtensions.AddBindings (MvvmCross.Binding.BindingContext.IMvxBindingContextOwner view, System.Object target, System.Collections.Generic.IEnumerable1[T] bindings, System.Object clearKey) [0x0001d] in :0
at MvvmCross.Binding.BindingContext.MvxBindingContextOwnerExtensions.AddBindings (MvvmCross.Binding.BindingContext.IMvxBindingContextOwner view, System.Object target, System.Collections.Generic.IEnumerable1[T] bindingDescriptions, System.Object clearKey) [0x00018] in <ceed92cfad5c4f51a9ee3f2991e6d83d>:0 at MvvmCross.Binding.BindingContext.MvxBindingContextOwnerExtensions.AddBinding (MvvmCross.Binding.BindingContext.IMvxBindingContextOwner view, System.Object target, MvvmCross.Binding.Bindings.MvxBindingDescription bindingDescription, System.Object clearKey) [0x0000b] in <ceed92cfad5c4f51a9ee3f2991e6d83d>:0 at MvvmCross.Binding.BindingContext.MvxBaseFluentBindingDescription1[TTarget].Apply () [0x0001f] in :0
at MvvmCross.Binding.BindingContext.MvxFluentBindingDescriptionSet`2[TOwningTarget,TSource].Apply () [0x00016] in :0
[...]
```

From the stack trace description, I tracked it down to TargetBinding for UITextView trying to parse arguments for subscribing to events:
https://github.com/MvvmCross/MvvmCross/blob/41b8aa8c9e433510cec2b1f5f439388c74c8c37f/MvvmCross/Binding/iOS/Target/MvxUITextViewTextTargetBinding.cs#L56

Expected behavior :thinking:

View should be presented with TextView in it, and RaisePropertyChanged is triggered every time a character is typed.

Actual behavior :bug:

Fatal exception as soon as the view is about to be presented.

Configuration :wrench:

Version: 5.6.2

Platform:

  • [X] :iphone: iOS
  • [ ] :robot: Android
  • [ ] :checkered_flag: WPF
  • [ ] :earth_americas: UWP
  • [ ] :apple: MacOS
  • [ ] :tv: tvOS
  • [ ] :monkey: Xamarin.Forms
ios bug

All 6 comments

And as soon as I submitted it, I see that @nmilcoff may already have fixed this in this commit: https://github.com/MvvmCross/MvvmCross/commit/976f83823ec8cd1d9b428be75a20c03d0f5126e0

Sorry about that, if this is the case.

This has been fixed in #2460 :) Thank you for reporting it anyways.

As a workaround you can copy the target binding from the source code, but _watch out!_ You also need to modify your LinkerPleaseInclude for it to work when linking (see: #2481).

I think we need to release 5.6.3 asap 馃槵.

I still get an exception when I try to create a binding for the Text property on the UITextView.
If I remove the Line set.Bind(this.MessageTextView).To(vm => vm.Message); everything works.

Code:

var set = this.CreateBindingSet<ContactView, ContactViewModel>();

set.Bind(this.NameTextField).To(vm => vm.Name);
set.Bind(this.FirstNameTextField).To(vm => vm.Firstname);
set.Bind(this.EmailTextField).To(vm => vm.Email);
set.Bind(this.MessageTextView).To(vm => vm.Message);
set.Bind(this.SendButton).To(vm => vm.SendFeedbackCommand);

set.Apply();

LinkerPleaseInclude:

...
        public void Include(UITextView textView)
        {
            textView.Text = textView.Text + "";            
            textView.Changed += (sender, args) => { textView.Text = ""; };            
        } 
...

Version:
5.6.3

Error:
missing source event info in MvxWeakEventSubscription
Parameter name: sourceEventInfo

Stacktrace:
at MvvmCross.Platform.WeakSubscription.MvxWeakEventSubscription`2[TSource,TEventArgs]..ctor (UIKit.NSTextStorage source, System.Reflection.EventInfo sourceEventInfo, System.EventHandler`1[TEventArgs] targetEventHandler) [0x00017] in <6adc0d5857264558a9d45778a78ae02a>:0 at MvvmCross.Platform.WeakSubscription.MvxWeakEventSubscription`2[TSource,TEventArgs]..ctor (UIKit.NSTextStorage source, System.String sourceEventName, System.EventHandler`1[TEventArgs] targetEventHandler) [0x00012] in <6adc0d5857264558a9d45778a78ae02a>:0 at MvvmCross.Platform.WeakSubscription.MvxWeakSubscriptionExtensionMethods.WeakSubscribe[TSource,TEventArgs] (TSource source, System.String eventName, System.EventHandler`1[TEventArgs] eventHandler) [0x00000] in <6adc0d5857264558a9d45778a78ae02a>:0 at MvvmCross.Binding.iOS.Target.MvxUITextViewTextTargetBinding.SubscribeToEvents () [0x00053] in C:\projects\mvvmcross\MvvmCross\Binding\iOS\Target\MvxUITextViewTextTargetBinding.cs:56 at MvvmCross.Binding.Bindings.MvxFullBinding.CreateTargetBinding (System.Object target) [0x00057] in C:\projects\mvvmcross\MvvmCross\Core\Binding\Bindings\MvxFullBinding.cs:160 at MvvmCross.Binding.Bindings.MvxFullBinding..ctor (MvvmCross.Binding.MvxBindingRequest bindingRequest) [0x00028] in C:\projects\mvvmcross\MvvmCross\Core\Binding\Bindings\MvxFullBinding.cs:63 at MvvmCross.Binding.Binders.MvxFromTextBinder.BindSingle (MvvmCross.Binding.MvxBindingRequest bindingRequest) [0x00000] in C:\projects\mvvmcross\MvvmCross\Core\Binding\Binders\MvxFromTextBinder.cs:55 at MvvmCross.Binding.Binders.MvxFromTextBinder+<>c__DisplayClass2_0.<Bind>b__0 (MvvmCross.Binding.Bindings.MvxBindingDescription description) [0x00000] in C:\projects\mvvmcross\MvvmCross\Core\Binding\Binders\MvxFromTextBinder.cs:37 at System.Linq.Enumerable+SelectArrayIterator`2[TSource,TResult].MoveNext () [0x0003a] in <3ccacafc6c3a44509d2050a54b68cc48>:0 at MvvmCross.Binding.BindingContext.MvxBindingContextOwnerExtensions.AddBindings (MvvmCross.Binding.BindingContext.IMvxBindingContextOwner view, System.Object target, System.Collections.Generic.IEnumerable`1[T] bindings, System.Object clearKey) [0x0001d] in C:\projects\mvvmcross\MvvmCross\Core\Binding\BindingContext\MvxBindingContextOwnerExtensions.cs:65 at MvvmCross.Binding.BindingContext.MvxBindingContextOwnerExtensions.AddBindings (MvvmCross.Binding.BindingContext.IMvxBindingContextOwner view, System.Object target, System.Collections.Generic.IEnumerable`1[T] bindingDescriptions, System.Object clearKey) [0x00018] in C:\projects\mvvmcross\MvvmCross\Core\Binding\BindingContext\MvxBindingContextOwnerExtensions.cs:86 at MvvmCross.Binding.BindingContext.MvxBindingContextOwnerExtensions.AddBinding (MvvmCross.Binding.BindingContext.IMvxBindingContextOwner view, System.Object target, MvvmCross.Binding.Bindings.MvxBindingDescription bindingDescription, System.Object clearKey) [0x0000b] in C:\projects\mvvmcross\MvvmCross\Core\Binding\BindingContext\MvxBindingContextOwnerExtensions.cs:79 at MvvmCross.Binding.BindingContext.MvxBaseFluentBindingDescription`1[TTarget].Apply () [0x0001f] in <866b1e46764b48aab0d408952a6f006f>:0 at MvvmCross.Binding.BindingContext.MvxFluentBindingDescriptionSet`2[TOwningTarget,TSource].Apply () [0x00016] in <866b1e46764b48aab0d408952a6f006f>:0

@kenigj Hey, I'm having the same error, did you find a solution to it? Thanks

Was this page helpful?
0 / 5 - 0 ratings