我想你會想建立一個Action委託,這樣你就可以寫出MyServiceHandler.GetServerTime(result => ...)
。我喜歡將其設置是這樣的:
void ShowServerTime_ButtonClick()
{
MyServiceHandler.GetServerTime(result => {
// do something with "result" here
});
}
public class MyServiceHandler
{
// wire up the handler in the constructor
static MyServiceHandler()
{
WCFService.GetServerTimeCompleted += (sender, args)
{
// assume you're going to pass the callback delegate in the User State:
var handler = args.UserState as Action<string>;
if (handler != null) handler(args.Result);
}
}
public static string GetServerTime(Action<string> callback)
{
// send the callback so that the async handler knows what to do:
WCFService.GetServerTimeAsync(callback)
}
}
當然,因爲你使用.NET 4.5/Silverlight的5個工作,你可以深入到async/await stuff,這是很好的語法糖(如果你進入的是之類的事情)。
謝謝,我不能使用異步等待或任務爲基礎的服務生成,因爲我在Xamarin iOS編程不支持它我不認爲。 – Neal