0

假设我的 xaml 中有 3 个图像视图。我正在从服务器下载图像,现在我想在我的图像视图中设置这些图像。我怎样才能做到这一点 ?请帮忙 !我的代码:

   void webClient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
    {
        //parse data
            var container = DeserializeFromJson<DataJsonAttributeContainer>(e.Result);

            //load into list
            for (int i = 0; i < container.MyBookList.Count; i++)
            {
                newData[i] = new data();
                newData[i].id = container.MyBookList[i].ID;
                newData[i].title = container.MyBookList[i].TITLE;
                newData[i].type = container.MyBookList[i].TYPE;
                newData[i].price = container.MyBookList[i].PRICE;
                newData[i].downloadLink = container.MyBookList[i].DOWNLOADLINK;
                string file_name = newData[i].downloadLink.ToString();
                string image_uri = "http://www.banglanews24.com/images/imgAll/" + file_name;   
                WebClient wc = new WebClient();
                wc.OpenReadCompleted += new OpenReadCompletedEventHandler(wc_OpenReadCompleted);
                wc.OpenReadAsync(new Uri(image_uri), wc);
            }

    void wc_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
    {

        if (e.Error == null && !e.Cancelled)
        {
            try
            {   //I can set just one image here....what should I do ?

                BitmapImage image = new BitmapImage();
                image.SetSource(e.Result);
                image1.Source = image;

            }
            catch (Exception ex)
            {
                //Exception handle appropriately for your app  
            }
        }
        else
        {
            //Either cancelled or error handle appropriately for your app  
        }  
    }
4

1 回答 1

2

这应该这样做:

   void webClient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
    //parse data
        var container = DeserializeFromJson<DataJsonAttributeContainer>(e.Result);

        //load into list
        for (int i = 0; i < container.MyBookList.Count; i++)
        {
            newData[i] = new data();
            newData[i].id = container.MyBookList[i].ID;
            newData[i].title = container.MyBookList[i].TITLE;
            newData[i].type = container.MyBookList[i].TYPE;
            newData[i].price = container.MyBookList[i].PRICE;
            newData[i].downloadLink = container.MyBookList[i].DOWNLOADLINK;
            string file_name = newData[i].downloadLink.ToString();
            string image_uri = "http://www.banglanews24.com/images/imgAll/" + file_name;   

            Uri uri = new Uri(image_uri, UriKind.Relative);
            ImageSource imgSource = new BitmapImage(uri);

            if (i==0) image1.source = imgSource;
            else if (i==1) image2.source = imgSource;
            else if (i==2) image3.source = imgSource;
            etc
        }

你会发现,当你给它一个 Image URI 时,你的图片会自动下载。

于 2012-10-02T09:28:49.513 回答