[英]WP7 Binding Image control to an image URI that needs cookie
我正在開發一個使用需要登錄的RESTful Web服務的應用程序。 此登錄使用會話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;
}
}
但是,返回的一些數據是用於顯示在不同ListBoxes
圖像縮略圖的URI。 在我不得不使用登錄Web服務之前,我只是將ImageSource
綁定到數據中的指定URI:
<Image Source="{Binding Icon_Url}" />
但是,現在我必須使用登錄,我必須在獲取圖像時提供cookie。 因此,我認為IValueConverter
會執行這個技巧,我將傳遞URI,然后使用我的擴展WebClient
下載BitmapImage
,直到我重新編寫我只能在WP7上使用WebClient
進行異步調用。
如何在LisBoxItem
中下載圖像控件的圖像,該Image
控件必須在請求中包含cookie?
謝謝!
您可以使用http類下載圖像並手動設置圖像:
var stream = httpResponse.GetResponseStream();
var bitmap = new BitmapImage();
bitmap.SetSource(stream);
image.Source = bitmap;
但這不適用於簡單的XAML綁定...(但您可以將此邏輯包裝在附加屬性中)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.