简体   繁体   English

WP7绑定图像控件到需要cookie的图像URI

[英]WP7 Binding Image control to an image URI that needs cookie

I am developing an app that consumes RESTful web services that needs login. 我正在开发一个使用需要登录的RESTful Web服务的应用程序。 This login is verified with session cookies and to download any data with a WebClient , I have used the following extension: 此登录使用会话cookie进行验证,并使用WebClient下载任何数据,我使用了以下扩展名:

public class CookieWebClient : WebClient
{
    [SecuritySafeCritical]
    public CookieWebClient() : base()
    {
    }

    protected override WebRequest GetWebRequest(Uri address)
    {
        var request = base.GetWebRequest(address);
        if (request is HttpWebRequest)
        {
            (request as HttpWebRequest).CookieContainer = App.GlobalCookieContainer;
        }
        return request;
    }
}

However, some of the data returned is URIs for image thumbnails that are shown in different ListBoxes . 但是,返回的一些数据是用于显示在不同ListBoxes图像缩略图的URI。 Before I had to use login for the web services, I simply just bound the ImageSource to the specified URI in the data: 在我不得不使用登录Web服务之前,我只是将ImageSource绑定到数据中的指定URI:

<Image Source="{Binding Icon_Url}" />

However, now that I have to use login, I have to provide a cookie when fetching the image. 但是,现在我必须使用登录,我必须在获取图像时提供cookie。 I thus thought that an IValueConverter would do the trick, where I would pass in the URI, which would then download the BitmapImage with my extended WebClient , right until I remebered that I can only do asynchronous calls with WebClient on WP7. 因此,我认为IValueConverter会执行这个技巧,我将传递URI,然后使用我的扩展WebClient下载BitmapImage ,直到我重新编写我只能在WP7上使用WebClient进行异步调用。

So my question is 所以我的问题是

How do I download images for a Image controls in a LisBoxItem that has to have a cookie in the request? 如何在LisBoxItem中下载图像控件的图像,该Image控件必须在请求中包含cookie?

Thank you! 谢谢!

You can download the image with the http classes and set the image manually: 您可以使用http类下载图像并手动设置图像:

var stream = httpResponse.GetResponseStream(); 
var bitmap = new BitmapImage();
bitmap.SetSource(stream);
image.Source = bitmap;

But this will not work with simple XAML bindings... (but you can wrap this logic in an attached property) 但这不适用于简单的XAML绑定...(但您可以将此逻辑包装在附加属性中)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM