【问题标题】:WP7 Binding Image control to an image URI that needs cookieWP7 将图像控件绑定到需要 cookie 的图像 URI
【发布时间】:2012-08-07 02:18:12
【问题描述】:

我正在开发一个使用需要登录的 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;
    }
}

但是,返回的某些数据是图像缩略图的 URI,它们以不同的ListBoxes 显示。在我不得不为 Web 服务使用登录之前,我只是将 ImageSource 绑定到数据中的指定 URI:

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

但是,既然我必须使用登录,我必须在获取图像时提供一个 cookie。因此,我认为IValueConverter 可以解决问题,我将在其中传递 URI,然后使用扩展的WebClient 下载BitmapImage,直到我记得我只能使用@987654329 进行异步调用@在 WP7 上。

所以我的问题是

我如何下载LisBoxItem 中必须在请求中包含cookie 的Image 控件的图像?

谢谢!

【问题讨论】:

  • 为什么异步调用会阻止您使用 WebClient?
  • @igrali 我认为这会阻止他在IValueConverter 中使用它,而不是一般情况下。
  • @Nate 哦,我明白了,好吧...我猜他不应该使用 IValueConverter 吗? :-)
  • 可能有更好的方法,但您可以尝试将它们缓存在独立存储中并从那里绑定。
  • @Nate 我们最近拜访了我公司的一位 Microsoft WP 开发人员,他谈到了很多关于 WP7 中的图像的问题。事实证明,图像的处理在内存方面非常糟糕,这就是为什么我希望内置控件尽可能地处理图像以避免内存泄漏。

标签: c# image windows-phone-7 xaml webclient


【解决方案1】:

您可以使用 http 类下载图像并手动设置图像:

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

但这不适用于简单的 XAML 绑定...(但您可以将此逻辑包装在附加属性中)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多