使用JSON.Net从Web下载后,在视图中设置图像

时间:2012-10-02 08:10:37

标签: c# image windows-phone-7 json.net

假设我的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  
        }  
    }

1 个答案:

答案 0 :(得分: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
        }

您会发现,当您为其提供图像URI时,将自动下载您的图像。