我有兩個「嵌套的」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事件處理程序之前觸發?
現在,我的解決方法是將我的ViewDissapearing事件處理程序代碼包裝在NSTimer.CreateScheduledTimer(0.5,delegate {})中;這是醜陋的,但這是我發現得到我想要的行爲的唯一途徑... – 2012-04-12 06:03:51