Data binding MvvmCross AttributedText转换器数据绑定生成空指针警告,但标签呈现良好--怎么了?

Data binding MvvmCross AttributedText转换器数据绑定生成空指针警告,但标签呈现良好--怎么了?,data-binding,xamarin.ios,xamarin,mvvmcross,Data Binding,Xamarin.ios,Xamarin,Mvvmcross,我有一个MvxTableViewCell,其中包含一个UILabel,我想在其中绑定/设置AttributeText。我所做的似乎是可行的(在屏幕上),但我也发现了这些错误: 2014-12-16 17:35:59.626 clientTouch[51481:1311271]MvxBind:错误:13.25 NotificationAttributeText的binding AttributeText在绑定执行过程中出现问题-问题TargetInvocationException:调用的目标已引

我有一个MvxTableViewCell,其中包含一个UILabel,我想在其中绑定/设置AttributeText。我所做的似乎是可行的(在屏幕上),但我也发现了这些错误:

2014-12-16 17:35:59.626 clientTouch[51481:1311271]MvxBind:错误:13.25 NotificationAttributeText的binding AttributeText在绑定执行过程中出现问题-问题TargetInvocationException:调用的目标已引发异常。
在/Developer/MonoTouch/Source/mono/mcs/class/corlib/System.Reflection/MonoMethod.cs:238中的System.Reflection.MonoMethod.Invoke(System.Object obj、BindingFlags invokeAttr、System.Reflection.Binder Binder、System.Object[]参数、System.Globalization.CultureInfo-culture)处
在/Developer/MonoTouch/Source/mono/mcs/class/corlib/System.Reflection/MethodBase.cs:114中的System.Reflection.MethodBase.Invoke(System.Object obj,System.Object[]参数)[0x00000]处
在Cirries.MvvmCross.Binding.Bindings.Target.MvxPropertyInfoTargetBinding.SetValueImpl(System.Object目标,System.Object值)[0x00000]处,输入:0
在Cirriary.MvvmCross.Binding.Bindings.Target.MvxConvertingTargetBinding.SetValue(System.Object值)[0x00000]处,输入:0
在cirrium.MvvmCross.Binding.Bindings.MvxFullBinding.UpdateTargetFromSource(System.Object值)[0x00000]中:0
InnerException为ArgumentNullException:参数不能为null。
参数名称:value
在MudioCu.UIabel.StIQuestDeTrue(Mythouc.Positual.NasAtdidieStand value)[0x0900b] in /Deave/MonoTouch /Soal/StimouCou/SrC/Buff/Copa/ UIKit /UIabel.G.Cs:272
at(包装器管理为本机)System.Reflection.monmethod:InternalInvoke(System.Reflection.monmethod,object,object[],System.Exception&)
在/Developer/MonoTouch/Source/mono/mcs/class/corlib/System.Reflection/MonoMethod.cs:230中的System.Reflection.monmethod.Invoke(System.Object obj、BindingFlags invokeAttr、System.Reflection.Binder Binder、System.Object[]参数、System.Globalization.CultureInfo culture)[0x00044]处
产生上述问题的具体约束是:

this.DelayBind(()=>{
var set=this.CreateBindingSet();
set.Bind(nameilabel)
.For(v=>v.AttributeText)
.To(vm=>vm.NotificationAttributeText)
.通过转换(“StringToAttributedTextTest”);
…这里有更多绑定。。。
}
真正的StringToAttributedText代码有点长,所以我制作了一个非常简单的代码(StringToAttributedTextTest),它会产生同样的问题:

公共类StringToAttributedTextTestConverter:MvxValueConverter
{
受保护的覆盖NSMutableAttributeString转换(
字符串值,
类型targetType,
对象参数,
System.Globalization.culture(信息文化)
{
返回新的NSMutableAttributeString(“hi honey”,UIFont.FromName(“Courier”,13f));
}
}
就像我说的,它呈现的很好,但是它确实产生了那些警告,这对我来说很糟糕

鉴于上面的值“converter”忽略了数据源,它似乎指向了一个错误的值转换器实现,但此时它只是一行代码

进一步隔离这个问题,当我将绑定替换为仅使用常规文本,并将其与视图模型中的普通旧字符串绑定时,我根本没有收到任何警告(当然,我以这种方式丢失了属性化文本)

set.Bind(NameUiLabel).For(v=>v.Text).To(vm=>vm.notificationAttributeText);
非常感谢您的帮助!

将生成警告,因为当重新使用
MvxTableViewCell
时,其绑定上下文将设置为null。这将导致绑定使用其回退值-默认情况下也是
null

要解决此问题(删除警告),可以尝试在单元格内为绑定设置
string.Empty
回退值


有关回退行为的更多信息,请搜索
UnsetValue
-Mvx尝试在该区域模拟WPF。

对于那些想要了解Stuart帮助的详细信息的人,以下是我如何消除警告的

在View类中,我创建了一个blankAttribString:

private NSAttributedString blankAttribString;
并将其初始化为具有普通字体/大小的空属性字符串:

blankAttribString = new NSMutableAttributedString(string.Empty, UIFont.FromName("Courier", 13f));
blankAttribString的作用就是提供一个“nothing”——所以string.Empty是我传递到NSAttributedString中的内容。最后,当涉及到绑定时间时,它看起来是这样的:

set.Bind(NameUiLabel)
    .For(v => v.AttributedText)
    .To(vm => vm.NotificationAttributedText)
    .WithConversion("StringToAttributedText")
    .WithFallback(blankAttribString);
上面,“StringToAttributedText”转换器接受一个常规字符串,并以特定于应用程序的方式生成一个属性字符串……但问题并不是如何完成这一部分,而是如何让回退开始工作


希望这能有所帮助。

谢谢斯图尔特。使用回退有效——尽管我最终不得不使用一个预先创建的blankAttributedString(基本上是一个愚蠢的属性字符串,它是一个带有字体的空字符串)。我也有同样的问题,但无法理解您的解决方法。粘贴修复代码plz@BerntHabermeier@Niniea希望有帮助。
set.Bind(NameUiLabel)
    .For(v => v.AttributedText)
    .To(vm => vm.NotificationAttributedText)
    .WithConversion("StringToAttributedText")
    .WithFallback(blankAttribString);