2010-09-23 1 views
0

У меня есть ColorAnimation внутри Раскадровки. После этого Раскадровка заканчивается, я хочу изменить свойство ColorAnimation внутри него. К сожалению, все атрибуты отправителя, похоже, защищены от записи, поэтому как я могу обойти это? Вот код:Как обойти защиту от записи в событиях?

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)), 
    } 

Вот стек-следа:

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: 
+0

Привет, Что происходит при выполнении этого кода? (добавьте полную трассировку стека исключения, если таковые имеются) –

+0

Я приложил стек-трассировку (перевел первую часть на английский). – Hedge

ответ

0

Я не уверен, что это сработает, но попробовать это (он будет использовать поток пользовательского интерфейса, чтобы сделать работу):

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)),  
})); 

Надеюсь, это поможет.

Manitra.

+0

В результате возникает синтаксическая ошибка: «Выражение« Лямбда »не может быть преобразовано в« System.Delegate », потому что это не тип делегата – Hedge

+0

Хорошо, теперь это лучше? –

+0

Он по-прежнему жалуется на защиту от записи. – Hedge

Смежные вопросы