WPF图片加载动画

3

我正在尝试使 WPF 中的图像控件在加载源时进行动画处理。

我已经找到了一个解决方案,但它是为 Windows 8 Metro 开发准备的,其中包含 ImageOpened 事件,这个事件在普通的 WPF 图像控件中并不存在。

你知道如何让图像在加载时显示加载动画的解决方案吗?

也许有一些带有这种类型的图像控件的库?

这是我在 Win 8 dev 中找到的 ImageLoader 控件:

<Grid>
    <Image x:Name="imgDisplay" Source="{Binding ElementName=parent,Path=Source}"
           ImageFailed="OnImageFailed"
           ImageOpened="OnImageOpened" />
    <ContentControl
        Visibility="{Binding ElementName=parent,Path=IsLoading,Converter={StaticResource converter}}"
        Content="{Binding ElementName=parent,Path=LoadingContent}"
        VerticalContentAlignment="Stretch"
        HorizontalContentAlignment="Stretch" />
    <ContentControl Visibility="{Binding ElementName=parent,Path=IsFailed,Converter={StaticResource converter}}"
                    Content="{Binding ElementName=parent,Path=FailedContent}"
                    VerticalContentAlignment="Stretch"
                    HorizontalContentAlignment="Stretch" />
</Grid>

以下是缺失的事件处理程序:

void OnImageOpened(object sender, RoutedEventArgs e)
{
    this.IsLoading = false;
    this.IsLoaded = true;
}

1
如果您的“Source”是“BitmapSource”,则可以尝试使用BitmapSource.DownloadCompleted事件。 - dkozl
1个回答

2

简单的解决方案:将 Image 控件放置在显示加载动画的控件上方(例如,将两者都放在同一个 Grid 中)。只要图像没有完全加载,它就是透明的,让加载动画显示出来,加载完成后自动隐藏动画。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接