2012-06-19 21 views
1

我在MonoTouch對話框的MvvmCross實現中使用DateElement。發生異常的原因是DateTimeElement中的方法UpdateDetailDisplay(UITableViewCell單元格)期望單元格參數永不爲空。MvvmCross - 在MonoTouch對話框中使用DateTimeElement時的空異常

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

看來,這個方法是對話視圖的設置過程中調用了三次:

  1. 上結合

    創建的DateElement

  2. 一個實例的結果

  3. 在構建TableView時調用GetCell。

的電池參數僅出現在事件3

難道我做錯了什麼或應該的方法有參數爲空像StringElement有一個測試?

這是我在viewDidLoad中事件代碼在我MvxTouchDialogViewController衍生:

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(字符串標題,日期時間日期) :基座(標題,日期) { }

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

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

回答

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找到這個記錄這是一個問題,然後解決它不久...

謝謝 - 和非常詳細的註釋

+0

登錄到https://github.com/slodge/MvvmCross/issues/13 – Stuart

+0

我認爲這是一種恭維。只是爲了確認,單元格參數始終爲空,直到TableView綁定到源代碼並開始調用source.GetCell(...) –

+0

Phew - 它的意圖是讚美 - 作爲感謝:) – Stuart

相關問題