[英]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
进行异步调用。
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.