在Windows Phone 8上访问OpenReadCompleted方法中的标题

时间:2013-01-25 14:39:37

标签: c# windows-phone-7.1 windows-phone-8

我有一个应用程序可以将图像下载到手机上,并根据图像类别将其分配给新闻源。我正在使用这个功能:

    private static void DownloadImage(string furl, string ids)
    {
        // Connect Again to the API
        WebClient client = new WebClient();
        client.Headers["NewsID"] = ids;

        string url = "www.xxx.com/image/xyz";

        client.OpenReadCompleted += new OpenReadCompletedEventHandler(client_OpenReadCompleted);
        client.OpenReadAsync(new Uri(url));
    }

    private static void client_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
    {
        using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
        {
            if (e.Error == null && !e.Cancelled)
            {
                Stream reply = null;
                StreamReader s = null;

                // i am not able to read the sender who is a webclient to retrieve the information it is always skipping it 
                WebClient wcd = sender as WebClient;

                reply = (Stream)e.Result;
                s = new StreamReader(reply);
                //Console.WriteLine(s.ReadToEnd());
                s.Close();
                reply.Close();

                if (!myIsolatedStorage.DirectoryExists("ImageCache"))
                {
                    myIsolatedStorage.CreateDirectory("ImageCache");
                }

                //try
                //{//((MS.Internal.InternalMemoryStream)(e.Result)).FinalUri.Segments[2]
                var graphImage = e.Result;
                Random rand = new Random();
                string fileName = string.Format("ImageCache/{0}.jpg", rand.Next());
                IsolatedStorageFileStream fileStream = myIsolatedStorage.CreateFile(fileName);

                BitmapImage image = new BitmapImage();
                image.SetSource(e.Result);
                WriteableBitmap wb = new WriteableBitmap(image);

                // Encode WriteableBitmap object to a JPEG stream.
                Extensions.SaveJpeg(wb, fileStream, wb.PixelWidth, wb.PixelHeight, 0, 85);
                fileStream.Close();

                //}
                //catch (IsolatedStorageException ex)
                //{
                //IsolatedStorageException
                //Exception handle appropriately for your app  
                //}
            }
        }
    }

在我用于下载图像的OpenReadComplete函数中,我想从标题中获取newsID,然后在将其保存到数据库之前将其分配给图像。我似乎无法访问标题。这可能吗?

1 个答案:

答案 0 :(得分:0)

我认为你可以和发件人一起阅读。

WebClient c = (WebClient)sender;
        string id = c.Headers["NewsID"];