【问题标题】:Images not downloading on second load in WPF在 WPF 中第二次加载时未下载图像
【发布时间】:2011-02-13 05:25:39
【问题描述】:

我有一个 WPF 应用程序,它有一个名为 MyBook 的 UserControl,在加载时会触发一个后台线程来获取域对象列表,每个域对象都有一个指向托管在 blob 存储中的 Azure 图像的 URL。

对于我返回的每个域对象,我添加了一个名为 LazyImageControl 的自定义控件的新实例,它将在后台从 Azure 下载图像并在完成后呈现图像。

这很好用,但是当我向场景中添加第二个 MyBook 控件时,由于某种原因图像无法加载,我无法弄清楚这是为什么。

这是LazyImageControl的代码

public LazyImageControl()
    {
        InitializeComponent();

        DataContextChanged += ContextHasChanged;
    }

    private void ContextHasChanged(object sender, DependencyPropertyChangedEventArgs e)
    {
        // Start a thread to download the bitmap...
        _uiThreadDispatcher = Dispatcher.CurrentDispatcher;
        new Thread(WorkerThread).Start(DataContext);
    }

    private void WorkerThread(object arg)
    {
        var imageUrlString = arg as string;
        string url = imageUrlString;

        var uriSource = new Uri(url);
        BitmapImage bi;
        if (uriSource.IsFile)
        {
            bi = new BitmapImage(uriSource);
            bi.Freeze();
            _uiThreadDispatcher.Invoke(DispatcherPriority.Send, new DispatcherOperationCallback(SetBitmap), bi);
        }
        else
        {
            bi = new BitmapImage();
            // Start downloading the bitmap...
            bi.BeginInit();
            bi.UriSource = uriSource;
            bi.UriCachePolicy = new RequestCachePolicy(RequestCacheLevel.Default);
            bi.DownloadCompleted += DownloadCompleted;
            bi.DownloadFailed += DownloadFailed;
            bi.EndInit();
        }

        // Spin waiting for the bitmap to finish loading...
        Dispatcher.Run();
    }

    private void DownloadFailed(object sender, ExceptionEventArgs e)
    {
        throw new NotImplementedException();
    }

    private void DownloadCompleted(object sender, EventArgs e)
    {
        // The bitmap has been downloaded. Freeze the BitmapImage
        // instance so we can hand it back to the UI thread.
        var bi = (BitmapImage)sender;
        bi.Freeze();

        // Hand the bitmap back to the UI thread.
        _uiThreadDispatcher.Invoke(DispatcherPriority.Send, new DispatcherOperationCallback(SetBitmap), bi);

        // Exit the loop we are spinning in...
        Dispatcher.CurrentDispatcher.InvokeShutdown();
    }

    private object SetBitmap(object arg)
    {
        LazyImage.Source = (BitmapImage)arg;
        return null;
    }

所以问题是,在 WorkerThread 第一次运行良好之后执行此操作,但我从未收到对 DownloadCompletedDownloadFailed 方法的回调,我不知道为什么......

有什么想法吗?

【问题讨论】:

    标签: wpf http azure


    【解决方案1】:

    不确定,但也许您应该在设置 BitmapImage.UriSource 之前尝试附加 DownloadCompletedDownloadFailed 事件处理程序,这应该会触发图像的加载,所以它可能是在您的事件处理程序被加载之前加载的附加(不是第一次,因为加载需要一段时间,但图像被缓存并会立即加载)

    另外:LazyImageControl 继承自哪个类,如果不是,我可以对其进行测试?

    【讨论】:

    • 我试试看,LazyImageControl 扩展了 UserControl,它只是一个图像的包装控件
    • 还是不行,我以为就这样了!但没有运气。我已将图像放在 PC 上的本地驱动器中,但仍然没有运气...
    • 好的,看起来我的问题更大,因为我在 BG 线程上创建了一个 BitmapImage 并试图将其传递给 UI 线程,但由于某种原因我无法克隆或冻结它,所以我最后只是将一个 byte[] 传递给 UI 线程并使用它构建一个图像......
    猜你喜欢
    • 2015-05-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多