OnAuthenticateCompleted

OnAuthenticateCompleted

我需要在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)
       }
 }

我正在将Service方法移至异步模型的过程中,Authenticate由BeginAuthenticate(), EndAuthenticate(), AuthenticateAsync(), OnAuthenticateOperationCompleted()Authenticate()组成。

当所有这些都完成后,我需要在LoginViewController上运行OnAuthenticateCompleted(),所以我将使用BeginInvokeOnMainThread(delegate....
这就是我卡住的地方。

如何从services类执行的LoginViewController类实例中获取方法OnAuthenticateCompleted()

编辑:解决方案:

添加了一个OnAuthenticateCompleted事件处理程序,该事件处理程序连接在Login()中,并调用了AuthenticateAsync()方法而不是Authenticate()。
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();
            }
     }

最佳答案

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

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

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

09-28 00:42