title: Converting UWP XAML animation to C#
body: <p>I have a working XAML animation for animating an odometer-like digit.  I need to make six almost identical, but different, versions of this animation.  So far as I can tell there is not a nice way to reuse animation XAML in this way, so to keep the code DRY I turn to a programatic creation of the animation.</p>
  
  <p>In trying to convert this XAML animation to C# I've run into an issue where my target can't be found:</p>
  
  <blockquote>
    <p>No installed components were detected.\r\n\r\nCannot resolve TargetName Digit1.</p>
  </blockquote>
  
  <p>If I call <code>Storyboard.SetTarget()</code> instead of <code>Storyboard.SetTargetName()</code> I no longer get this exception but still nothing happens on the screen.</p>
  
  <p>Can someone tell me what I'm doing wrong in this translation?</p>
  
  <p>XAML:</p>
  
  <pre><code>&lt;Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        mc:Ignorable="d"&gt;
  
      &lt;Page.Resources&gt;
          &lt;x:Double x:Key="OdometerMoveUpDistance"&gt;-101&lt;/x:Double&gt;
  
          &lt;Storyboard x:Name="OdometerUpStoryboard"&gt;
              &lt;DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(CompositeTransform.TranslateY)"
                                             Storyboard.TargetName="Digit1"&gt;
                  &lt;EasingDoubleKeyFrame KeyTime="0" Value="0" /&gt;
                  &lt;EasingDoubleKeyFrame KeyTime="0:0:1" Value="{StaticResource OdometerMoveUpDistance}" /&gt;
              &lt;/DoubleAnimationUsingKeyFrames&gt;
          &lt;/Storyboard&gt;
      &lt;/Page.Resources&gt;
  
      &lt;Canvas x:Name="Odometer" Width="100" Height="100"&gt;
          &lt;TextBlock x:Name="Digit1" FontSize="100" Text="8"&gt;
              &lt;TextBlock.RenderTransform&gt;&lt;CompositeTransform /&gt;&lt;/TextBlock.RenderTransform&gt;
          &lt;/TextBlock&gt;
      &lt;/Canvas&gt;
  
  &lt;/Page&gt;
  </code></pre>
  
  <p>C#:</p>
  
  <pre><code>private void Btn_Click(object sender, RoutedEventArgs e)
  {
      DoubleAnimationUsingKeyFrames digitUp = CreateOdometerdigitAnim(this.Digit1, 0, -101);
  
      var storyboard = new Storyboard();
      storyboard.Children.Add(digitUp);
      storyboard.Begin(); // Exception happens on this line
  }
  
  private DoubleAnimationUsingKeyFrames CreateOdometerdigitAnim(TextBlock digit, double startPos, double endPos)
  {
      var start = new EasingDoubleKeyFrame();
      start.KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromMilliseconds(0));
      start.Value = startPos;
  
      var end = new EasingDoubleKeyFrame();
      end.KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromMilliseconds(1000));
      end.Value = endPos;
  
      var anim = new DoubleAnimationUsingKeyFrames();
      anim.KeyFrames.Add(start);
      anim.KeyFrames.Add(end);
  
      Storyboard.SetTargetProperty(anim, "(UIElement.RenderTransform).(CompositeTransform.TranslateY)");
      Storyboard.SetTargetName(anim, digit.Name);
  
      /* Using SetTarget instead of SetTargetName throws no exception but still does not animate */
      // Storyboard.SetTarget(anim, digit);
  
      return anim;
  }
  </code></pre>