2012-10-29 200 views
2

我需要在MonoTouch中异步进行web服务调用,因为UIAlertView只在工作完成后才显示出来。异步方法?

当前码(伪)

Class LoginviewController 
{ 
     void Login(credentials) 
     { 
      showAlert("Logging in") ; 
      bool authenticated = service.Authenticate(Credentials); 
     } 
}  

class Service 
{ 
     Public bool Authenticate(object Credentials) 
     { 
      object[] results = this.Invoke("GetAuth", Credentials) 
     } 
} 

我在移动服务的方法来异步模式,配合量由BeginAuthenticate(), EndAuthenticate(), AuthenticateAsync(), OnAuthenticateOperationCompleted()当然Authenticate()的身份验证的过程。

当所有这些都完成了,我需要在其上运行LoginViewController OnAuthenticateCompleted(),所以我会用BeginInvokeOnMainThread(delegate....

这是我卡住。

如何从服务类中执行LoginViewController类实例中的方法OnAuthenticateCompleted()

编辑:解决方法:

补充说,在登录()挂钩,并称为AuthenticateAsync()方法,而不是身份验证的OnAuthenticateCompleted事件处理程序()。

Class LoginviewController 
    { 
      void Login(credentials) 
      { 
       showAlert("Logging in") ; 
       service.AuthenticateCompleted += new GetAuthenticationCompletedEventHandler(OnAuthenticateCompleted); 
       service.AuthenticateAsync(Credentials); 
      } 

      public void OnAuthenticateCompleted(obj sender, GetAuthenticationCompletedEventArgs args) 
      { 
       bool authenticated = (bool)args.Results; 
       //do stuff 
       hideAlert(); 
      } 
    }  

回答

2

您不从服务类执行LoginViewController.OnAuthenticateCompleted,您在完成的事件处理程序中执行它。

class LoginViewController 
{ 
    void Login (credentials) 
    { 
     service.AuthenticateAsync (credentials, LoginCompletedCallback); 
     } 

    } 
    void LoginCompletedCallback() 
    { 
     BeginInvokeOnMainThread (OnAuthenticateCompleteded); 
    } 
}