Mobile Button animation error: Cannot resolve TargetName BetterButton RRS feed

  • Question

  • I am trying to create a button planeProjection StoryBoard Animation Progamatically. The buttons will be created on the fly not in xmlns 

    I started with the sample phone app. Here is the code that I am using:

    using System;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Animation;
    using Microsoft.Phone.Controls;
    namespace WindowsPhoneApplication1
        public partial class MainPage : PhoneApplicationPage
            public class BetterButton : Button
                public DoubleAnimation da;
                public Storyboard sb;
                public Double YRot
                        PlaneProjection pp = Projection as PlaneProjection;
                        return pp.RotationY;
                        PlaneProjection pp = Projection as PlaneProjection;
                        pp.RotationY = value;
                public void Start_Animation()
                    // If the Storyboard is running and you try to change
                    // properties of its animation objects programmatically, 
                    // an error will occur.
                    // Change the TargetName of the animation to the name of the
                    // object that was clicked.
                    da.SetValue(Storyboard.TargetNameProperty, this.Name);
                    // Begin the animation.
                protected override void OnIsPressedChanged(DependencyPropertyChangedEventArgs e)
                    DependencyProperty p = e.Property;
                        Boolean b = (Boolean) e.NewValue;
                        if (b)
            // Constructor
            public MainPage()
            static Image _ButtonImage;
            public static Image ButtonImage
                    if (_ButtonImage == null)
                        _ButtonImage = new Image();
                        System.Windows.Media.Imaging.BitmapImage imgSource = new System.Windows.Media.Imaging.BitmapImage();
                        imgSource.UriSource = new Uri("ApplicationIcon.png", UriKind.RelativeOrAbsolute);
                        _ButtonImage.Source = imgSource;
                    return _ButtonImage;
            public void BuildButtons()
                BetterButton b = new BetterButton();
                b.Projection = new PlaneProjection();
                b.Visibility = System.Windows.Visibility.Visible;
                b.Name = "BetterButton";
                b.da = new DoubleAnimation();
                b.da.From = 0.0;
                b.da.To = 360.0;
                b.da.AutoReverse = true;
                b.sb = new Storyboard();
                PropertyPath path = new PropertyPath("(BetterButton.YRot)");            
                b.sb.SetValue(Storyboard.TargetPropertyProperty, path);
                b.Content = ButtonImage;
                b.FontFamily = new FontFamily("Arial");
                b.FontSize = 30;
                b.MaxWidth = 200;
                b.MaxHeight = 100;
                b.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
                b.Arrange(new Rect(20, 10, 220, 110));
                //   b.Click += new RoutedEventHandler(new RoutedEventHandler(b_Click));
                //   PageTitle.AddHandler(UIElement.MouseLeftButtonDownEvent, new RoutedEventHandler(b_Click), true);

    I get the following error when I click on the button

    System.InvalidOperationException was unhandled
      Message=Cannot resolve TargetName BetterButton.
           at MS.Internal.XcpImports.MethodEx(IntPtr ptr, String name, CValue[] cvData)
           at MS.Internal.XcpImports.MethodEx(DependencyObject obj, String name)
           at MS.Internal.XcpImports.Storyboard_Begin(Storyboard storyboard)
           at System.Windows.Media.Animation.Storyboard.Begin()
           at WindowsPhoneApplication1.MainPage.BetterButton.Start_Animation()
           at WindowsPhoneApplication1.MainPage.BetterButton.OnIsPressedChanged(DependencyPropertyChangedEventArgs e)
           at System.Windows.Controls.Primitives.ButtonBase.OnIsPressedPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
           at System.Windows.DependencyObject.RaisePropertyChangeNotifications(DependencyProperty dp, Object newValue, Object oldValue)
           at System.Windows.DependencyObject.SetValueInternal(DependencyProperty dp, Object value, Boolean allowReadOnlySet, Boolean isSetByStyle, Boolean isSetByBuiltInStyle, PropertyInvalidationReason reason)
           at System.Windows.DependencyObject.SetValueInternal(DependencyProperty dp, Object value, Boolean allowReadOnlySet)
           at System.Windows.Controls.Primitives.ButtonBase.set_IsPressed(Boolean value)
           at System.Windows.Controls.Primitives.ButtonBase.OnMouseLeftButtonDown(MouseButtonEventArgs e)
           at System.Windows.Controls.Control.OnMouseLeftButtonDown(Control ctrl, EventArgs e)
           at MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj, IntPtr unmanagedObjArgs, Int32 argsTypeIndex, String eventName)

    Wednesday, June 8, 2011 2:36 PM


All replies