【问题标题】:DoubleAnimation for image with Storyboard.TargetProperty="Height"带有 Storyboard.TargetProperty="Height" 的图像的 DoubleAnimation
【发布时间】:2014-12-06 19:43:57
【问题描述】:

我已阅读How can I make an Image grow in size (give the illusion of selection) through a WPF animation? 并尝试使用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。

【问题讨论】:

    标签: c# xaml windows-phone windows-phone-8.1


    【解决方案1】:

    您的代码中只有一个小错误,通过设置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();
    }
    

    【讨论】:

    • 我想使用 Stretch="None" 所以不是整个图像都可见。尝试了您的解决方案,即使我设置了 Stretch="uniform",它也不起作用。您使用的是 Windows Phone Toolkit 还是 Windows Phone Silverlight?
    • 我使用了 Windows Phone Silverlight。如果您只想“隐藏”图像的一部分,而不是在更改高度时拉伸它,例如将图像放在 StackPanel 中并为 StackPanel 的高度设置动画
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-05-02
    • 1970-01-01
    • 1970-01-01
    • 2019-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多