我有一个包含 UILabel 的 MvxTableViewCell,我想在其中绑定(bind)/设置 attributedText。我所用的似乎有效(在屏幕上),但同时我也遇到了这些错误:
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
产生上述问题的特定绑定(bind)是这样的:
this.DelayBind (() => {
var set = this.CreateBindingSet<NotificationsCellView, NotificationActivitySubViewModel>();
set.Bind(NameUiLabel)
.For(v => v.AttributedText)
.To(vm => vm.NotificationAttributedText)
.WithConversion("StringToAttributedTextTest");
... more bindings here ...
}
真正的 StringToAttributedText 代码有点长,所以我做了一个非常简单的代码 (StringToAttributedTextTest),它产生了同样的问题:
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));
}
}
就像我说的——它呈现得很好,但它确实会产生那些警告,这对我来说似乎很糟糕。
鉴于上述值“转换器”完全忽略了数据源,它似乎指向一个不正确的值转换器实现,但此时它只是一行代码!
进一步隔离问题,当我将绑定(bind)替换为仅使用常规文本,并将其与 View 模型中的普通旧字符串绑定(bind)时,我根本没有收到任何警告(当然我以这种方式丢失了属性文本)。
set.Bind(NameUiLabel).For(v => v.Text).To(vm => vm.NotificationAttributedText);
非常感谢任何帮助!
最佳答案
将生成警告,因为当重用 MvxTableViewCell
时,其绑定(bind)上下文将设置为 null。这将导致绑定(bind)使用它们的回退值 - 默认情况下也是 null
。
要解决此问题(删除警告),您可以尝试为单元格内的绑定(bind)设置 string.Empty
回退值。
有关回退行为的更多信息,请搜索 UnsetValue
- Mvx 试图在该区域模仿 WPF。
关于data-binding - MvvmCross AttributedText Converter 数据绑定(bind)生成空指针警告,但标签呈现正常——有什么问题吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27517300/