我有一个WPF图像控件,它的源属性绑定到返回Image对象的属性“ImageSrc”。WPF BitmapImage DownloadCompleted event never raised
<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事件是从来没有提出这样一行:
vm.Method();
永远不会执行,因此该方法( )在我的视图模型从未执行。
我已经检查,当我使用视图模型中绑定的ImageSrc属性更新来自Image对象的Source属性时,转换器正确执行。
我在做什么错?
ok,但是您的解决方案确保了Method()仅在图像呈现,准备就绪且可见/显示给用户时才执行?我需要一种方法,只有在用户可以看到图像后才执行方法,而不是之前。通过在ImageSource属性中调用Method,我不确定仅在图像对用户可见后才会调用它。可能会出现Method()方法在图像显示之前的几秒钟执行并且用户可见。 – user1624552
请参阅已编辑的答案,了解如何仅调用一次Method()。当渲染线程最终在屏幕上显示图像时,Afaik无法获得通知。这是你可以得到的最好的。 – Clemens