The MvvmCross AttributedText Converter data binding generates a null pointer warning, but the label renders fine - what's wrong?

I have an MvxTableViewCell that contains a UILabel where I would like to bind / set an attribute. What I have is working (on screen), but I am getting these errors along with all of this:

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 the question that is causing 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 actual StringToAttributedText code is a bit long, so I made a super trivial (StringToAttributedTextTest) that causes the same problem:

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));
    }
}

      

As I said, it looks great, but it generates those warnings that seem bad to me.

Given the above meaning, the "converter" ignores the datasource altogether, it seems to indicate the wrong implementation of the value converter, but it's just a line of code at this point!

Further isolating the problem, when I replace the binding to just use plain text, and binding it to a plain old string from the view model, I don't get any warnings at all (of course, I lose the attributed text this way).

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

Any help would be much appreciated!

+3


source to share


2 answers


Warnings will be thrown because when a is MvxTableViewCell

reused then its binding context is null. This will cause the bindings to use their fallback values, which are the default as well null

.

To work around this issue (to remove warnings), you can try setting string.Empty

fallback values ​​for your cell bindings.



For more on Fallback behavior, look for UnsetValue

- Mvx is trying to mimic WPF in this area.

+1


source


For those looking for more information on Stewart's help, here's how I got rid of the warnings.

In the view class, I create a blankAttribString:

private NSAttributedString blankAttribString;

      

and initialize it with an empty string with a simple font / size:

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

      



The point of blankAttribString is only there to enforce "nothing" - so string.Empty is what I am passing to NSAttributedString. Finally, when it comes to binding time, this is how it looks:

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

      

Above, the "StringToAttributedText" converter takes a regular string and generates the supplied string in a specific way for the application ... but the question is not how to do that. It was about making sleep work.

Hope it helps.

0


source







All Articles