Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MvvmCross AttributedText Converter data binding generates null pointer warning, but label renders fine -- what is wrong?

I've got a MvxTableViewCell that contains a UILabel where I'd like to bind / set the attributedText on. What I have appears to work (on screen), but I'm getting these errors along side of it all:

2014-12-16 17:35:59.626 clientTouch[51481:1311271] MvxBind:Error: 13.25 Problem seen during binding execution for binding AttributedText for NotificationAttributedText - problem TargetInvocationException: Exception has been thrown by the target of an invocation.
      at System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x0005c] in /Developer/MonoTouch/Source/mono/mcs/class/corlib/System.Reflection/MonoMethod.cs:238 
  at System.Reflection.MethodBase.Invoke (System.Object obj, System.Object[] parameters) [0x00000] in /Developer/MonoTouch/Source/mono/mcs/class/corlib/System.Reflection/MethodBase.cs:114 
  at Cirrious.MvvmCross.Binding.Bindings.Target.MvxPropertyInfoTargetBinding.SetValueImpl (System.Object target, System.Object value) [0x00000] in <filename unknown>:0 
  at Cirrious.MvvmCross.Binding.Bindings.Target.MvxConvertingTargetBinding.SetValue (System.Object value) [0x00000] in <filename unknown>:0 
  at Cirrious.MvvmCross.Binding.Bindings.MvxFullBinding.UpdateTargetFromSource (System.Object value) [0x00000] in <filename unknown>:0 
InnerException was ArgumentNullException: Argument cannot be null.
Parameter name: value
      at MonoTouch.UIKit.UILabel.set_AttributedText (MonoTouch.Foundation.NSAttributedString value) [0x0000b] in /Developer/MonoTouch/Source/monotouch/src/build/compat/UIKit/UILabel.g.cs:272 
  at (wrapper managed-to-native) System.Reflection.MonoMethod:InternalInvoke (System.Reflection.MonoMethod,object,object[],System.Exception&)
  at System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00044] in /Developer/MonoTouch/Source/mono/mcs/class/corlib/System.Reflection/MonoMethod.cs:230

The specific binding in question that is producing the above issue is this:

this.DelayBind (() => {
       var set = this.CreateBindingSet<NotificationsCellView, NotificationActivitySubViewModel>();

       set.Bind(NameUiLabel)
          .For(v => v.AttributedText)
          .To(vm => vm.NotificationAttributedText)
          .WithConversion("StringToAttributedTextTest");

       ... more bindings here ...

   }

The real StringToAttributedText code is a bit long, so I made a super trivial one (StringToAttributedTextTest) which produces the same issue:

public class StringToAttributedTextTestConverter: MvxValueConverter<string, NSMutableAttributedString>
{
    protected override NSMutableAttributedString Convert(
             string value, 
             Type targetType, 
             object parameter, 
             System.Globalization.CultureInfo culture)
    {
        return new NSMutableAttributedString("hi honey", UIFont.FromName("Courier", 13f));
    }
}

Like I said -- it renders fine, but it does generate those warnings, which seem bad to me.

Given the above value "converter" is ignoring the data source all together it would seem to point at an incorrect value converter implementation, but it's just a line of code at this point!

Further isolating the issue, when I replace the binding to just use regular text, and binding that with a plain old string from the view model, I get no warnings at all (of course I loose the attributed text that way).

set.Bind(NameUiLabel).For(v => v.Text).To(vm => vm.NotificationAttributedText);

Any help would be highly appreciated!

like image 758
Bernie Habermeier Avatar asked Nov 26 '25 18:11

Bernie Habermeier


2 Answers

The warnings will be generated because when an MvxTableViewCell is reused, then its binding context is set to null. This will cause the bindings to use their fallback values - which by default are also null.

To workaround this issue (to remove the warnings), you could try setting string.Empty fallback values for your bindings within the cell.

For more on Fallback behavior, search for UnsetValue - Mvx tries to mimic WPF in this area.

like image 59
Stuart Avatar answered Nov 29 '25 19:11

Stuart


For those that want more detail on Stuart's help, here is how I got rid of the warnings.

In the View Class, I create a blankAttribString:

private NSAttributedString blankAttribString;

and initialize it to an empty attributed string with a plain font / size:

blankAttribString = new NSMutableAttributedString(string.Empty, UIFont.FromName("Courier", 13f));

The point of blankAttribString is just there to provide a 'nothing' -- so string.Empty is what I pass into the NSAttributedString. Finally, when it comes to binding time, here is how that looks like:

set.Bind(NameUiLabel)
    .For(v => v.AttributedText)
    .To(vm => vm.NotificationAttributedText)
    .WithConversion("StringToAttributedText")
    .WithFallback(blankAttribString);

Above, "StringToAttributedText" converter takes a regular string and generates an attributed string in an app-specific way... but the question wasn't really about how to do that part of it. It was really about how to get the fallback to work.

Hope that helps.

like image 32
Bernie Habermeier Avatar answered Nov 29 '25 18:11

Bernie Habermeier



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!