【问题标题】:MvvmCross - Null Exception when using DateTimeElement in MonoTouch-DialogMvvmCross - 在 MonoTouch-Dialog 中使用 DateTimeElement 时出现空异常
【发布时间】:2012-06-19 16:16:35
【问题描述】:

我在 MonoTouch Dialog 的 MvvmCross 实现中使用 DateElement。发生异常是因为 DateTimeElement 中的方法 UpdateDetailDisplay(UITableViewCell cell) 期望 cell 参数永远不会为空。

    protected override void UpdateDetailDisplay(UITableViewCell cell)
    {
        if (cell.DetailTextLabel != null)
        {
            cell.DetailTextLabel.Text = FormatDate(Value);
        }
    }

在设置Dialog视图的过程中,这个方法好像被调用了3次:

  1. 作为创建 DateElement 实例的结果

  2. 关于绑定

  3. 在构建 TableView 时调用 GetCell。

单元格参数仅出现在事件 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 派生我自己的类来“解决”这个问题:

公共类 MyDateElement : DateElement { 公共 MyDateElement(字符串标题,DateTime 日期) : 基础(标题、日期) { }

    protected override void UpdateDetailDisplay(UITableViewCell cell)
    {
        if(cell == null)return;

        if (cell.DetailTextLabel != null)
        {
            cell.DetailTextLabel.Text = FormatDate(Value);
        }
    }
}

【问题讨论】:

    标签: c# xamarin.ios monotouch.dialog mvvmcross


    【解决方案1】:

    这看起来像是 MonoTouch.Dialog 和/或 MvvmCross 中的某个错误。

    我做错了吗?

    没有。在我看来,你做的事情是对的。

    我猜这个错误出现在您的示例中,因为 DateTimeElement 在列表中的位置很靠后 - 因此在第一次绘制表格时它不在屏幕上(没有单元格)。


    我不清楚最好的解决方案是你找到的那个,还是要更改 ValueElement 中调用 UpdateXXXDisplay 以首先检查 null 的代码(或者是否要防御并同时做这两个!)

        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 中记录这个问题,然后尽快修复它...

    感谢您找到这个 - 以及非常详细的说明

    【讨论】:

    • 我认为这是一种恭维。只是为了确认一下,在 TableView 绑定到源并开始调用 source.GetCell(...) 之前,单元格参数始终为 null
    • 唷 - 这是一种恭维 - 并作为感谢:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-21
    • 2014-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多