-1

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"));
4

1 回答 1

-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;
}
于 2013-03-08T23:37:34.497 回答