【问题标题】:How to change EasingDoubleKeyFrame value at Runtime?如何在运行时更改 EasingDoubleKeyFrame 值?
【发布时间】:2015-02-05 05:20:22
【问题描述】:

我正在尝试制作 TranslateTransform 动画。在动画中,我需要我的对象保持在窗口的中心。我知道 WPF 动画是可冻结的。我正在使用转换器,但它会在启动时初始化值。有没有办法在运行时设置 EasingDoubleKeyFrame 的值?

这是我的 XAML 代码:

<Storyboard x:Key="Storyboard1">
    <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.X)" Storyboard.TargetName="grid">
        <EasingDoubleKeyFrame KeyTime="0" Value="{Binding Width, Converter={StaticResource Minus}, ElementName=grid}"/>
        <EasingDoubleKeyFrame KeyTime="0:0:1" Value="{Binding Width, Converter={StaticResource EnteringValueConverter}, ElementName=grid}"/>
    </DoubleAnimationUsingKeyFrames>
</Storyboard>

【问题讨论】:

    标签: c# wpf xaml animation expression-blend


    【解决方案1】:

    为 EasingDoubleKeyFrame 元素添加一个 x:Name 属性。 之后您应该可以通过代码访问它:

    x:Name="关键帧"

    然后:

    keyframe.SetValue(EasingDoubleKeyFrame.ValueProperty, yourValue);

    【讨论】:

    • 我将 x:Name 属性添加到 EasingDoubleKeyFrame 元素,但我无法从后面的代码访问它。 后面的代码中不存在任何名为 myEasingKey 的内容。
    【解决方案2】:

    “这是因为动画是可冻结的对象。在MSDN Documentation 中有更多信息,但基本上这意味着您不能使用绑定,因为冻结对象(即动画)中的属性无法更改。

    要解决此限制,您需要在代码隐藏中完成部分或全部工作。"

    引用Stackoverflow

    【讨论】:

      【解决方案3】:

      EasingDoubleKeyFrame 不能通过x:Name 属性访问。相反,您应该使用不同的方法将EasingDoubleKeyFrame 定义为StaticResource 并通过ResourceKey 链接它,如下所示:

      <UserControl.Resources>
          <EasingDoubleKeyFrame x:Key="myEasingKey" KeyTime="0:0:2" Value="136" />
          <Storyboard x:Key="Storyboard1">
              <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.X)"
                                             Storyboard.TargetName="rectangle">
                   <StaticResource ResourceKey="myEasingKey" />
              </DoubleAnimationUsingKeyFrames>
          </Storyboard>
      </UserControl.Resources>
      

      通过下面的代码访问它:

      (EasingDoubleKeyFrame)Resources["myEasingKey"].Value = X;
      

      希望这会有所帮助...

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多