none
How to animate objects created during runtime in WPF? RRS feed

  • Question

  • Hello, I am trying to animate an Image object created in code-behind during runtime in my WPF application. I already have a storyboard created in XAML for generic usage by Image objects (Fade in effects), so I created an event for the Image object.

     

    void Image_Loaded(object sender, RoutedEventArgs e)
            {
                Image obj = (Image)sender;
                Storyboard sbImageFadeIn = (Storyboard)this.FindResource("sbImageFadeIn");
                sbImageFadeIn.SetValue(Storyboard.TargetNameProperty, obj.Name);
                sbImageFadeIn.Begin();
            }
    

     

     However, an exception occurs on "sbImageFadeIn.Begin();". By the way, the Image object is named "asd".

    System.InvalidOperationException was unhandled
      Message='asd' name cannot be found in the name scope of 'KinectBLEND.ImageGrid'.
      Source=PresentationFramework
      StackTrace:
           at System.Windows.Media.Animation.Storyboard.ResolveTargetName(String targetName, INameScope nameScope, DependencyObject element)
           at System.Windows.Media.Animation.Storyboard.ClockTreeWalkRecursive(Clock currentClock, DependencyObject containingObject, INameScope nameScope, DependencyObject parentObject, String parentObjectName, PropertyPath parentPropertyPath, HandoffBehavior handoffBehavior, HybridDictionary clockMappings, Int64 layer)
           at System.Windows.Media.Animation.Storyboard.ClockTreeWalkRecursive(Clock currentClock, DependencyObject containingObject, INameScope nameScope, DependencyObject parentObject, String parentObjectName, PropertyPath parentPropertyPath, HandoffBehavior handoffBehavior, HybridDictionary clockMappings, Int64 layer)
           at System.Windows.Media.Animation.Storyboard.BeginCommon(DependencyObject containingObject, INameScope nameScope, HandoffBehavior handoffBehavior, Boolean isControllable, Int64 layer)
           at System.Windows.Media.Animation.Storyboard.Begin()
           at KinectBLEND.ImageGrid.Image_Loaded(Object sender, RoutedEventArgs e) in C:\Users\Sarabande\Documents\Expression\Blend 4\Projects\KinectBLEND\KinectBLEND\ImageGrid.xaml.cs:line 67
           at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
           at System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args)
           at MS.Internal.FrameworkObject.OnLoaded(RoutedEventArgs args)
           at System.Windows.BroadcastEventHelper.BroadcastEvent(DependencyObject root, RoutedEvent routedEvent)
           at System.Windows.BroadcastEventHelper.BroadcastLoadedEvent(Object root)
           at System.Windows.Media.MediaContext.FireLoadedPendingCallbacks()
           at System.Windows.Media.MediaContext.FireInvokeOnRenderCallbacks()
           at System.Windows.Media.MediaContext.RenderMessageHandlerCore(Object resizedCompositionTarget)
           at System.Windows.Media.MediaContext.RenderMessageHandler(Object resizedCompositionTarget)
           at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
           at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler)
           at System.Windows.Threading.Dispatcher.WrappedInvoke(Delegate callback, Object args, Int32 numArgs, Delegate catchHandler)
           at System.Windows.Threading.DispatcherOperation.InvokeImpl()
           at System.Threading.ExecutionContext.runTryCode(Object userData)
           at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData)
           at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
           at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
           at System.Windows.Threading.DispatcherOperation.Invoke()
           at System.Windows.Threading.Dispatcher.ProcessQueue()
           at System.Windows.Threading.Dispatcher.WndProcHook(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
           at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
           at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)
           at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
           at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler)
           at System.Windows.Threading.Dispatcher.WrappedInvoke(Delegate callback, Object args, Int32 numArgs, Delegate catchHandler)
           at System.Windows.Threading.Dispatcher.InvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs)
           at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)
           at MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg)
           at System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame)
           at System.Windows.Application.RunInternal(Window window)
           at System.Windows.Application.Run()
           at KinectBLEND.App.Main() in C:\Users\Sarabande\Documents\Expression\Blend 4\Projects\KinectBLEND\KinectBLEND\obj\Debug\App.g.cs:line 0
           at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
           at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
           at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
           at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
           at System.Threading.ThreadHelper.ThreadStart()
      InnerException: 
    

     

    Thursday, November 24, 2011 2:31 AM

Answers

  • After some struggling, I realised the problem to the code: I don't need the name for the animation, instead I simply pass the Image object into the Begin() method.

     

    void Image_Loaded(object sender, RoutedEventArgs e)
            {
                Image obj = (Image)sender;
                Storyboard sbImageFadeIn = (Storyboard)this.FindResource("sbImageFadeIn");
                //sbImageFadeIn.SetValue(Storyboard.TargetNameProperty, obj.Name);
                sbImageFadeIn.Begin(obj);
            }
    

    • Marked as answer by Frostea Thursday, November 24, 2011 3:04 AM
    Thursday, November 24, 2011 3:03 AM

All replies

  • After some struggling, I realised the problem to the code: I don't need the name for the animation, instead I simply pass the Image object into the Begin() method.

     

    void Image_Loaded(object sender, RoutedEventArgs e)
            {
                Image obj = (Image)sender;
                Storyboard sbImageFadeIn = (Storyboard)this.FindResource("sbImageFadeIn");
                //sbImageFadeIn.SetValue(Storyboard.TargetNameProperty, obj.Name);
                sbImageFadeIn.Begin(obj);
            }
    

    • Marked as answer by Frostea Thursday, November 24, 2011 3:04 AM
    Thursday, November 24, 2011 3:03 AM
  • Hi Frostea,

    Glad to hear that you have resolved your issue, and thank you for sharing your solution here.

     

    Best regards,


    Sheldon _Xiao[MSFT]
    MSDN Community Support | Feedback to us
    Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, November 24, 2011 9:36 AM
    Moderator