Compile error while animating on Windows Phone

I am new to Windows Phone animation and am using the below code but it gives me compile error:

'System.Windows.Controls.Button' does not contain a definition for "BeginAnimation" and does not use the "BeginAnimation" extension method taking a first argument of type "System.Windows.Controls.Button" (are you missing a directive or assembly reference usage?)

Which link am I missing?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;

    private void button1_Click(object sender, RoutedEventArgs e)
        DoubleAnimation da = new DoubleAnimation();
        da.From = 30;
        da.To = 100;
        da.Duration = new Duration(TimeSpan.FromSeconds(1));
        button1.BeginAnimation(Button.HeightProperty, da);            



source to share

1 answer

The UIElement.BeginAnimation method does not exist in WP7. Instead, you will need to create a storyboard like this:

private void button1_Click(object sender, RoutedEventArgs e)
  var sb = new Storyboard();
  var db = CreateDoubleAnimation(30, 100,
         button1, Button.HeightProperty, TimeSpan.FromMilliseconds(1000));

private static DoubleAnimation CreateDoubleAnimation(double from, double to, 
      DependencyObject target, object propertyPath, TimeSpan duration)
  var db = new DoubleAnimation();
  db.To = to;
  db.From = from;
  db.Duration = duration;
  Storyboard.SetTarget(db, target);
  Storyboard.SetTargetProperty(db, new PropertyPath(propertyPath));
  return db;




All Articles