【问题标题】:How to animate height of control in Windows 8 XAML如何在 Windows 8 XAML 中为控件的高度设置动画
【发布时间】:2012-05-07 09:45:00
【问题描述】:

我在 Windows 8 上的 Metro 风格 C#/XAML 应用程序中执行基于故事板的简单控件高度动画时遇到问题。

以下琐碎的 XAML 和 sn-ps 背后的代码在 Silverlight 5 和 Windows Phone 7 中运行良好,但在 Windows 8 中没有任何作用(至少对我而言):

<Page.Resources>
    <Storyboard x:Name="expandAnimation">
        <DoubleAnimation Storyboard.TargetName="scaleButton" Storyboard.TargetProperty="Height" From="50" To="200" Duration="0:0:1"/>
    </Storyboard>
</Page.Resources>

<StackPanel Width="200">
    <Button x:Name="scaleButton" Click="scaleButton_Click" Content="Scale"/>
    <Button Content="Another button"/>
    <Button Content="Yet another button"/>
</StackPanel>

C#代码:

private void scaleButton_Click(object sender, RoutedEventArgs e)
{
    expandAnimation.Begin();
}

可以更改相同的代码来为控件的其他属性设置动画,例如按预期工作的不透明度。

我可以为 ScaleTransform 设置动画来进行缩放,但它会改变控件的内部渲染,并且不会影响相邻控件的布局,这对我来说是个问题。

希望我在这里没有遗漏任何明显的东西,但这不应该有效吗?

【问题讨论】:

    标签: xaml windows-8 windows-runtime winrt-xaml


    【解决方案1】:

    您只需要添加EnableDependentAnimation="True" 就可以了。

    【讨论】:

    • 非常感谢@Sofian!将EnableDependentAnimation="True" 添加到上述xaml 中的DoubleAnimation 元素就可以了。
    • 非常感谢,您能解释一下这个属性的作用吗?
    • 我浪费了半个小时让这种事情变得疯狂,谢谢!
    【解决方案2】:

    依赖动画是一种会导致 Xaml 重新布局的动画。昂贵的;因此需要“选择加入”。

    http://msdn.microsoft.com/en-us/library/windows/apps/xaml/windows.ui.xaml.media.animation.pointanimation.enabledependentanimation.aspx

    如果可能,您应该使用渲染变换并缩放元素的视觉效果。这是独立的,意味着页面上的其余元素不需要移动以适应。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-19
      • 2012-12-28
      • 2014-12-15
      • 2013-09-25
      相关资源
      最近更新 更多