DoubleAnimation for image with Storyboard.TargetProperty=&qu

本文关键字:qu TargetProperty Storyboard for image with DoubleAnimation | 更新日期: 2023-09-27 18:33:18

我已阅读如何通过 WPF 动画使图像增大大小(给人一种选择的错觉)? 并尝试用DoubleAnimation更改图像的高度,但不起作用。

<Image Name="image" Height="100" Source="http://www.online-image-editor.com/styles/2013/images/example_image.png" Stretch="None">
        <Image.Resources>
            <Storyboard x:Name="show">
                <DoubleAnimation
                    Storyboard.TargetName="image"
                    Storyboard.TargetProperty="Height"
                     From="100" To="400" Duration="0:0:1"
                    />
            </Storyboard>
        </Image.Resources>
    </Image>
    <Button Content="image stretch" Click="button_clicked" />

这是button_clicked函数:

private void button_clicked(object sender, RoutedEventArgs e)
{
    show.Begin();
}

[编辑]:因为Johan Falk的回答,我用Windows Phone Silverlight工具包尝试了上面的代码,它工作了。所以解决方案是使用Windows Phone Silverlight。

DoubleAnimation for image with Storyboard.TargetProperty=&qu

你的代码中只有一个小错误,通过设置Stretch="None"你不允许图像向任何方向拉伸,因此始终保持其原始大小。例如将Stretch更改为Uniform,它应该可以工作。

这是我用来验证它的示例代码:

<StackPanel x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
    <Image Source="/Assets/ApplicationIcon.png" x:Name="testImage" Stretch="Uniform">
        <Image.Resources>
            <Storyboard x:Name="Animation">
                <DoubleAnimation Storyboard.TargetName="testImage" Storyboard.TargetProperty="Height" From="100" To="200" Duration="0:0:1" />
            </Storyboard>
        </Image.Resources>
    </Image>
    <Button Content="Expand image" Grid.Row="1" Click="Button_Click"></Button>
</StackPanel>
private void Button_Click(object sender, RoutedEventArgs e)
{
    Animation.Begin();
}