2012-05-21 26 views
1

在我的Windows Phone應用程序中,我使用單例類來發送和接收Web請求和響應。因此,在我目前的實施中,我將調用viewmodel的網絡請求以及Action<>代表。爲了檢索錯誤回調,它對我來說工作得很好。我的問題是,當我快速切換應用程序的應用程序時,Web請求取消並返回一個Web錯誤。我需要在我的視圖模型中獲取此Web異常。我如何通過使用Func<>代表獲得此答覆?請任何人都幫我解決這個問題。在我的視圖模型中使用(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<> 
} 

回答

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); 
} 
+0

那麼什麼是使用函數功能<>的;當我們將使用這個Func <>時,我完全對這兩個問題感到困惑。 –

+0

@NithaPaul Func <>代表用於如果你想讓你的函數返回一些東西。如果你想返回null,則使用Action <>。例如,如果你有一個功能, int A() - >你應該使用func <>委託,因爲它返回一些值。 – daryal

相關問題