我想獲得一個異步委託。我創建了一個簡單的代碼來了解我的問題在哪裏。我一直在閱讀異步/等待文檔,但所有情況都很簡單。異步類委託(異步/等待)
我的代碼申請異步委託:
public override void ViewDidLoad()
{
base.ViewDidLoad();
int code;
CustomDialog dialog = new CustomDialog (this);
dialog.Select();
dialog.Finish += (t) =>
{
code = t.code;
};
//
//Wait until app executes dialog.finish().
//I don't want put the UIAlertView in "dialog.Finish".
//
UIAlertView alert = new UIAlertView ("dialog later", "item select " + code, null, null, "ok");
alert.Show();
}
public class CustomType
{
public int code { get; set; }
}
public class CustomDialog
{
public event DialogoEventHandle Finish;
public delegate void DialogoEventHandle (CustomType t);
private UIViewController view;
public CustomDialog(UIViewController view)
{
this.view = view;
}
protected void OnFinish(CustomType t)
{
if (Finish != null)
Finish(t);
}
public void Select()
{
ThreadPool.QueueUserWorkItem ((object sender) =>
{
//operation
Thread.Sleep (400);
this.view.InvokeOnMainThread (() =>
{
OnFinish (new CustomType() { code = 5 });
});
});
}
}
}
方法'CalculateAsync'被命名爲異步,但真的不是 –
@SriramSakthivel謝謝,但我有OnFinish委託的問題。 – DesarrollosDD