WPF BitmapImage DownloadCompleted事件从未引发 [英] WPF BitmapImage DownloadCompleted event never raised

查看:189
本文介绍了WPF BitmapImage DownloadCompleted事件从未引发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个WPF图像控件,它的源属性绑定到一个返回图像对象的属性 ImageSrc。

I have a WPF image control which its source property is bound to a property "ImageSrc" that returns an Image object.

<Window x:Class="My.Apps.WPF.Main"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:viewmodel="clr-namespace:My.Apps.WPF.ViewModels"
    xmlns:classes="clr-namespace:My.Apps.WPF.Classes" 
   >
    <Window.Resources> 
        <viewmodel:MyViewModel x:Key="myViewModel" />      
        <classes:ImgToSrcConverter x:Key="imgToSrcConverter" />       
    </Window.Resources>

    <Grid x:Name="TopGrid" DataContext="{StaticResource myViewModel}">

       <Image Grid.Row="0">
          <Image.Source>
             <MultiBinding NotifyOnTargetUpdated="True" Converter="{StaticResource imgToSrcConverter}">
                 <Binding Path="ImageSrc" />
                 <Binding Path="." />
              </MultiBinding>
          </Image.Source>
       </Image>
    </Grid>
</Window>

转换器

public class ImgToSrcConverter : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
    {
        Image image = values[0] as Image;
        if (image != null)
        {
            MemoryStream ms = new MemoryStream();
            image.Save(ms, image.RawFormat);
            ms.Seek(0, SeekOrigin.Begin);
            BitmapImage bi = new BitmapImage();
            bi.BeginInit();
            bi.StreamSource = ms;
            bi.EndInit();

            ViewModel vm = values[1] as ViewModel;
            bi.DownloadCompleted += (s, e) => 
            {
                vm.Method();
            };

            return bi;
        }
        return null;
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

我遭受的问题是BitmapImage的DownloadCompleted事件永远不会引发,所以行:

The problem I am suffering is that DownloadCompleted event for BitmapImage is never raised so line:

vm.Method();

从未执行过,因此我的视图模型中的Method()从未执行过。

is never executed and hence that Method() in my view model is never executed.

当我使用绑定在视图模型中的ImageSrc属性更新视图中的Image对象的Source属性时,我检查了转换器是否正确执行。

I have checked that the converter is executed correctly when I update Source property from Image object in view using the ImageSrc property bound in view model.

我在做什么错了?

推荐答案

我找到了一种方法,虽然不是最好的方法,但我整整一天的时间都在为绑定触发器和图像和流的可用方法所困扰。我最后要做的是分配一个全局布尔值,说图像已更改,然后使用Image.LayoutUpdated操作检查该布尔值。一旦看到布尔值并验证图像大小不为零,就会反转布尔值(这样就不会再次运行),并在加载/显示图像时执行需要做的事情。

I found a way to do this although not quite the best method but I struggled for about a whole day messing with binding triggers and available methods with images and streams. What I ended up doing was assigning a global boolean saying the image has been changed and then using the Image.LayoutUpdated action to check for that boolean. Once it sees the Boolean and verifies the image size is not zero, it inverts the boolean (so it doesn't run again) and does what needs to be done on Image loaded/in view.

这篇关于WPF BitmapImage DownloadCompleted事件从未引发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆