创建一个返回WebClient的类或函数

时间:2013-03-08 23:35:14

标签: c# webclient

我正在使用WebClient在我的代码上下载图像非常大,所以我试图将我的代码分成几类。有人能举例说明如何将代码放在类或函数中吗?

WebClient client = new WebClient ();
client.DownloadDataCompleted += 
    (object sender, DownloadDataCompletedEventArgs e) => 
    {
        byte[] result = e.Result;
        if (result != null) 
        {
            NSData data1 = NSData.FromArray (e.Result);
            UIImage img = UIImage.LoadFromData (data1);
            InvokeOnMainThread (delegate {
                avatar.Image = img;     
            });             
        }
   };

client.DownloadDataAsync(new Uri(
    "http://xx.xx.xx.xx/fbcache/" + 
    list[indexPath.Row].comentario_id_usuario + 
    ".jpg"));

1 个答案:

答案 0 :(得分:-1)

我认为你可能会对课程的内容和服务目的感到困惑。至于将该代码放入函数中,请尝试以下开始(尽管我怀疑您需要对正在进行的异步调用做些什么来获得您想要的结果):

public WebClient MyFunctionName()
{
        #region Baixando as imagens e as exibindo
        WebClient client = new WebClient ();
        client.DownloadDataCompleted += (object sender, DownloadDataCompletedEventArgs e) => {
            byte[] result = e.Result;
            if (result != null) {
                NSData data1 = NSData.FromArray (e.Result);
                UIImage img = UIImage.LoadFromData (data1);
                InvokeOnMainThread (delegate {
                    avatar.Image = img;


                });

            }
        };
        client.DownloadDataAsync (new Uri ("http://xx.xx.xx.xx/fbcache/"+list [indexPath.Row].comentario_id_usuario+".jpg"));
        #endregion
        return client;
}