使用(Func<>)在我的视图模型中回调错误

时间:2012-05-21 06:54:31

标签: c# windows-phone-7 silverlight-4.0 delegates

在我的Windows Phone应用程序中,我使用单例类来发送和接收Web请求和响应。因此,在我当前的实现中,我将调用来自viewmodel的网络请求以及Action<>代表。用于检索错误回调,它对我来说很好。我的问题是当我快速应用程序切换应用程序时,Web请求取消并返回Web错误。我需要在我的视图模型中获取此Web异常。如何使用Func<>代理获得此响应?请有人帮我解决这个问题。

// viewmodel code
private void Login()
{
    LoginContoller.Instance.Login(userName, password, ErrorCallbackCompleted);
}
//callback
private void ErrorCallbackCompleted()
{

}

// code inside singleton class
public static Action ErrorCallbackResponse;

public void Login (string userName, string password, Action errorCallback)
{
    ErrorCallbackResponse = errorCallback;
}

// This method will be invoked from the error callback of web request class
public void GetErrorCallBack(Exception ex) 
{
    ErrorCallbackResponse();
    //I need to pass this ex object to  my viewmodel using Func<>
}

1 个答案:

答案 0 :(得分:2)

// viewmodel code
  private void Login()
  {
       LoginContoller.Instance.Login(userName, password, ErrorCallbackCompleted);
  }

 //callback
 public void ErrorCallbackCompleted(Exception exception)
 {
 }

// code inside singleton class

  public static Action<Exception> ErrorCallbackResponse;

public void Login (string userName, string password, Action<Exception> errorCallback)
{
   ErrorCallbackResponse = errorCallback;
}

public void GetErrorCallBack(Exception ex) // This method will be invoked from the error callback of web request class
{
    ErrorCallbackResponse(ex);
}