我在MonoTouch Dialog的MvvmCross实现中使用DateElement。发生异常是因为DateTimeElement中的方法UpdateDetailDisplay(UITableViewCell单元格)要求cell参数永远不为null。
protected override void UpdateDetailDisplay(UITableViewCell cell)
{
if (cell.DetailTextLabel != null)
{
cell.DetailTextLabel.Text = FormatDate(Value);
}
}
在设置Dialog视图期间似乎调用了这个方法三次:
由于创建了DateElement的实例
关于绑定
在调用GetCell时构建TableView期间。
单元格参数仅出现在事件3上。
我做错了什么,或者该方法是否像StringElement一样测试参数为null?
以下是我在MvxTouchDialogViewController衍生物中的ViewDidLoad事件中的代码:
public override void ViewDidLoad ()
{
base.ViewDidLoad ();
this.Root = new RootElement("Sign-Up")
{
new Section()
{
Bind( new EntryElement("Gender:", "required"), "{'Value':{'Path':'Gender','Mode':'TwoWay'}}"),
Bind( new EntryElement("First name:", "required"), "{'Value':{'Path':'FirstName','Mode':'TwoWay'}}"),
Bind( new EntryElement("Last name:", "required"), "{'Value':{'Path':'LastName','Mode':'TwoWay'}}"),
Bind( new EntryElement("Display name:", "required"), "{'Value':{'Path':'DisplayName','Mode':'TwoWay'}}"),
Bind( new EntryElement("Email:", "required"), "{'Value':{'Path':'Email','Mode':'TwoWay'}}"),
Bind( new EntryElement("Confirm email:", "required"), "{'Value':{'Path':'ConfirmEmail','Mode':'TwoWay'}}"),
Bind( new EntryElement("Password:", "required",null,true), "{'Value':{'Path':'Password','Mode':'TwoWay'}}"),
Bind( new EntryElement("Confirm password:", "required", null,true), "{'Value':{'Path':'ConfirmPassword','Mode':'TwoWay'}}"),
Bind (new DateElement("Date of birth", DateTime.Now), "{'Value':{'Path':'DateOfBirth','Mode':'TwoWay'}}")
},
};
}
我只能通过自己的方法从DateElement派生自己的类来“解决”这个问题:
public class MyDateElement:DateElement { public MyDateElement(字符串标题,DateTime日期) :基数(标题,日期) { }
protected override void UpdateDetailDisplay(UITableViewCell cell)
{
if(cell == null)return;
if (cell.DetailTextLabel != null)
{
cell.DetailTextLabel.Text = FormatDate(Value);
}
}
}
答案 0 :(得分:0)
这看起来像是MonoTouch.Dialog和/或MvvmCross中的某个错误。
我做错了吗?
没有。在我看来,你正在做正确的事。
我想你的样本中会出现这个错误,因为DateTimeElement在列表中很长 - 所以当第一次绘制表时它不在屏幕上(不会得到单元格)。
我不清楚最好的解决方案是您找到的解决方案,还是更改ValueElement中的代码,该代码调用UpdateXXXDisplay来检查是否为空(或者是否为防御并同时执行这两项操作!)
private UITextAlignment _alignment;
public UITextAlignment Alignment
{
get { return _alignment; }
set { _alignment = value; UpdateCaptionDisplay(CurrentAttachedCell);}
}
private TValueType _value;
public TValueType Value
{
get { return _value; }
set { _value = value; UpdateDetailDisplay(CurrentAttachedCell); }
}
我会在https://github.com/slodge/MvvmCross/issues中将此问题记录为问题,然后很快修复...
感谢您找到这个 - 以及非常详细的说明