通过 wp8.1 中的代码隐藏自定义网格动画

animating a custom grid via code behind in wp8.1

我正在尝试使用网格创建动画。这是一个登录屏幕。每当用户点击忘记密码时,我希望第二个网格从顶部开始动画并滑动直到它停在中心并点击它的可见性变化。我知道如何使用 blend 来做到这一点,但问题是我有一种从代码背后做到这一点的冲动。为此,我正在使用双关键帧 class。很难知道动画第二个网格的代码背后的问题在哪里。不知道问题是什么以及如何制作动画需要如此认真的帮助。

下面是我的代码:

    Grid gd= this.FindName("SecondaryGrid") as Grid;
    DoubleAnimationUsingKeyFrames dm=new DoubleAnimationUsingKeyFrames();
    LinearDoubleKeyFrame l1=new LinearDoubleKeyFrame();
    LinearDoubleKeyFrame l2=new LinearDoubleKeyFrame();



    l1.Value=-703.203;
    l1.KeyTime=TimeSpan.FromSeconds(0);

    l2.Value=0;
    l2.KeyTime=TimeSpan.FromSeconds(1);

    dm.KeyFrames.Add(l1);
    dm.KeyFrames.Add(l2);

    dm.Duration=new Duration(TimeSpan.FromMilliseconds(3000));  

    Storyboard sb = new Storyboard();
    sb.Children.Add(dm);



      Storyboard.SetTarget(dm, gd);
      Storyboard.SetTargetName(dm, gd.Name);
      Storyboard.SetTargetProperty(dm, "Position");

      sb.Begin();
      SecondaryGrid.Visibility = Visibility.Visible;
Grid gd = this.FindName("SecondaryGrid") as Grid;
        DoubleAnimationUsingKeyFrames dm = new DoubleAnimationUsingKeyFrames();
        LinearDoubleKeyFrame l1 = new LinearDoubleKeyFrame();
        LinearDoubleKeyFrame l2 = new LinearDoubleKeyFrame();



        l1.Value = -703.203;
        l1.KeyTime = TimeSpan.FromSeconds(0);

        l2.Value = 0;
        l2.KeyTime = TimeSpan.FromSeconds(1);

        dm.KeyFrames.Add(l1);
        dm.KeyFrames.Add(l2);

        dm.Duration = new Duration(TimeSpan.FromMilliseconds(30000));

        Storyboard sb = new Storyboard();
        sb.Children.Add(dm);



        Storyboard.SetTarget(dm, gd);
        Storyboard.SetTargetName(dm, gd.Name);
        Storyboard.SetTargetProperty(dm, "(UIElement.RenderTransform).(CompositeTransform.TranslateY)");

        sb.Begin();

尽情享受吧:)

编辑

好吧,我所做的是去 XAML 并在其属性中创建一个 SecondaryGrid 现在去渲染变换并单击编辑并将 Y 值设置为 1 然后我在 XAML 中看到我们得到

    <Grid.RenderTransform>
         <CompositeTransform TranslateX="0" TranslateY="1"/>
  </Grid.RenderTransform>

所以从这里我知道因为没有位置 属性 所以我需要将 targetProperty 设置为渲染变换并且我应该更改 Y 属性 因为它需要来自顶部到 Composite 内部的底部 Transform.TranslateY