views:

35

answers:

1

I have a ColorAnimation inside a Storyboard. After that Storyboard finishes I want to change a property of the ColorAnimation within it. Sadly all sender's attributes seem to be write-protected, so how can I circumvent it? Here is the code:

storyboard.Completed += new EventHandler(storyboard_Completed);

    void storyboard_Completed(object sender, EventArgs e)
    {
        ClockGroup clock = sender as ClockGroup;
        Storyboard board = clock.Timeline as Storyboard;
        (board.Children[0] as ColorAnimation).To = Color.FromArgb(1, Convert.ToByte(random.Next(0, 255)), Convert.ToByte(random.Next(0, 255)), 
    }

Here's the stack-trace:

System.InvalidOperationException 
Message=No property can be assigned to his System.Windows.Media.Animation.ColorAnimation-object because it is write-protected.
  Source=WindowsBase
  StackTrace:
       bei System.Windows.DependencyObject.SetValueCommon(DependencyProperty dp, Object value, PropertyMetadata metadata, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue, OperationType operationType, Boolean isInternal)
       bei System.Windows.DependencyObject.SetValueInternal(DependencyProperty dp, Object value)
       bei InnovationsforumInfoterminal.SurfaceWindow1.storyboard_Completed(Object sender, EventArgs e) in D:\Projekte\Innovationsforum Infoterminal\InnovationsforumInfoterminal\SurfaceWindow1.xaml.cs:Zeile 86.
       bei System.Windows.Media.Animation.Clock.RaiseAccumulatedEvents()
       bei System.Windows.Media.Animation.TimeManager.RaiseEnqueuedEvents()
       bei System.Windows.Media.Animation.TimeManager.Tick()
       bei System.Windows.Media.MediaContext.RenderMessageHandlerCore(Object resizedCompositionTarget)
       bei System.Windows.Media.MediaContext.AnimatedRenderMessageHandler(Object resizedCompositionTarget)
       bei System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
       bei MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler)
       bei System.Windows.Threading.Dispatcher.WrappedInvoke(Delegate callback, Object args, Int32 numArgs, Delegate catchHandler)
       bei System.Windows.Threading.DispatcherOperation.InvokeImpl()
       bei System.Threading.ExecutionContext.runTryCode(Object userData)
       bei System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData)
       bei System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
       bei System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       bei System.Windows.Threading.DispatcherOperation.Invoke()
       bei System.Windows.Threading.Dispatcher.ProcessQueue()
       bei System.Windows.Threading.Dispatcher.WndProcHook(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
       bei MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
       bei MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)
       bei System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
       bei MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler)
       bei System.Windows.Threading.Dispatcher.WrappedInvoke(Delegate callback, Object args, Int32 numArgs, Delegate catchHandler)
       bei System.Windows.Threading.Dispatcher.InvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs)
       bei MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)
       bei MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg)
       bei System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame)
       bei System.Windows.Application.RunInternal(Window window)
       bei System.Windows.Application.Run()
       bei InnovationsforumInfoterminal.App.Main() in D:\Projekte\Innovationsforum Infoterminal\InnovationsforumInfoterminal\obj\Debug\App.g.cs:Zeile 0.
       bei System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       bei Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       bei System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
       bei System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       bei System.Threading.ThreadHelper.ThreadStart()
  InnerException: 
A: 

Hello,

I'm not sure it's gonna work but try this (it will use the UI thread to do the job) :

Dispatcher.BeginInvoke(new Action(() => {
  (board.Children[0] as ColorAnimation).To = Color.FromArgb(1, Convert.ToByte(random.Next(0, 255)), Convert.ToByte(random.Next(0, 255)),     
}));

I hope it helps.

Manitra.

Manitra Andriamitondra
This results in a syntax-error:"Lambda Expression" can't be converted to "System.Delegate", because it isn't a delegate-type
Hedge
Ok, is it any better now ?
Manitra Andriamitondra
It still complains about the write-protection.
Hedge
If it's still not responded tonigh, I'll have a look ... with VS launched :)
Manitra Andriamitondra
Thank you, I'll try it on my own in the meantime.
Hedge