2012-04-12 56 views
0

我有兩個「嵌套的」DialogViewControllers - 一個「視圖」和「編輯」。兩者都使用StringElements呈現來自內部數據結構的數據。在Edit DVC中,我鉤住了StringElement的Changed事件來更新我的內部數據結構。我還掛鉤編輯DVC的ViewDissapearing(sic)事件以重新呈現View DVC,並將編輯發送到雲服務。是否可以控制爲MonoTouch.Dialog中的事件觸發的EventHandlers的順序?

我的問題是Changed事件處理程序在ViewDissapearing事件處理程序後被調用,所以我的內部數據結構沒有及時更新。

,設置了DVC的看起來像這樣的代碼:

var root = RenderViewItem(ThisItem);    
    var dvc = new DialogViewController(root, true); 

    // create an Edit button which pushes the edit view onto the nav stack 
    dvc.NavigationItem.RightBarButtonItem = new UIBarButtonItem(UIBarButtonSystemItem.Edit, delegate { 
     var editRoot = RenderEditItem(ThisItem, true /* render the list field */); 
     editViewController = new DialogViewController(editRoot, true); 
     editViewController.ViewDissapearing += (sender, e) => 
     { 
      // trigger a sync with the service when the view disappears      
      App.ViewModel.SyncWithService(); 

      // reload the View page 
      dvc.BeginInvokeOnMainThread(() => 
      { 
       var oldroot = root; 
       root = RenderViewItem(ThisItem); 
       dvc.Root = root; 
       dvc.ReloadData(); 
       oldroot.Dispose(); 
      }); 
     };  
     controller.PushViewController(editViewController, true); 
    }); 

    // push the "view item" view onto the nav stack 
    controller.PushViewController(dvc, true); 

內RenderEditItem(),我添加了基於內部數據結構StringElements,並添加以通常的方式更改的事件處理程序:

stringElement.Changed += delegate { /* change a data structure */ }; 

有沒有辦法讓Changed事件處理程序在ViewDissapearing事件處理程序之前觸發?

+0

現在,我的解決方法是將我的ViewDissapearing事件處理程序代碼包裝在NSTimer.CreateScheduledTimer(0.5,delegate {})中;這是醜陋的,但這是我發現得到我想要的行爲的唯一途徑... – 2012-04-12 06:03:51

回答

0

好消息! MonoTouch.Dialog 5.2.11版本改變了EntryElement.Changed事件發生的方式 - 現在針對每個按鍵都會產生一個行爲,而不是僅在發生焦點更改時才產生。所以我的「競爭條件」不再發生,我現在能夠消除NSTimer黑客。

此更改的相關好處是,您不必再調用EntryElement.FetchValue()以確保EntryElement.Value是最新的。

http://docs.xamarin.com/ios/releases/MonoTouch_5/MonoTouch_5.2#11欲瞭解更多信息。

相關問題