使用代码移动对象并使用xaml

时间:2012-06-25 15:50:38

标签: c# wpf xaml expression-blend-4 visualstates

我正在使用混合4来制作一个wpf应用程序

我在xaml中定义了一个wrapPanel:

<WrapPanel x:Name="WrapPanel_ShowMe">

当我进入某个可视状态时,将wrapPanel后面的代码放到一个新位置:

WrapPanel_ShowMe.RenderTransform = new TranslateTransform(0,-661.5);

我这样做是因为在这种状态下不同的wrapPanels可以出现在状态中,(从复制状态中保存)

退出状态后,我将面板移回其原始位置。

我也有xaml在其他状态下移动相同的wrapPanel:

<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.Y)" Storyboard.TargetName="WrapPanel_ShowMe">
    <EasingDoubleKeyFrame KeyTime="0" Value="-648"/>
</DoubleAnimationUsingKeyFrames>

所以状态A移动c#中的wrapPanel,状态B使用xaml移动面板。

当我使用状态B时,wrapPanel会像它应该的那样移动而没有任何问题。 (XAML)

当我使用状态A时,wrapPanel会像它应该的那样移动而没有任何问题。 (C#)

当我使用状态A移动面板并且然后尝试使用状态B后面时出现问题。程序崩溃了,我不明白为什么。 似乎在使用代码隐藏来移动对象后,xaml渲染转换不再有效吗?

任何人都可以告诉我为什么会这样,以及如何解决/解决它?

提前致谢

修改

堆栈结果如下:

Unhandled Exception: System.InvalidOperationException: 'Children' property value in the path '(0).(1)[3].(2)' points to immutable instance of 'System.Windows.Media.TransformCollection'.
   at System.Windows.Media.Animation.Storyboard.VerifyPathIsAnimatable(PropertyPath path)
   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.VisualStateGroup.StartNewThenStopOld(FrameworkElement element, Storyboard[] newStoryboards)
   at System.Windows.VisualStateManager.GoToStateInternal(FrameworkElement control, FrameworkElement stateGroupsRoot, VisualStateGroup group, VisualState state, Boolean useTransitions)
   at Microsoft.Expression.Interactivity.Core.ExtendedVisualStateManager.TransitionEffectAwareGoToStateCore(FrameworkElement control, FrameworkElement stateGroupsRoot, String stateName, VisualStateGroup group, VisualState state, Boolean useTransitions, VisualTransition transition, Boolean animateWithTransitionEffect, VisualState previousState)
   at Microsoft.Expression.Interactivity.Core.ExtendedVisualStateManager.GoToStateCore(FrameworkElement control, FrameworkElement stateGroupsRoot, String stateName, VisualStateGroup group, VisualState state, Boolean useTransitions)
   at System.Windows.VisualStateManager.GoToStateCommon(FrameworkElement control, FrameworkElement stateGroupsRoot, String stateName, Boolean useTransitions)
   at Microsoft.Expression.Interactivity.VisualStateUtilities.GoToState(FrameworkElement element, String stateName, Boolean useTransitions)
   at System.Windows.Interactivity.TriggerBase.InvokeActions(Object parameter)
   at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
   at System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args)
   at System.Windows.Controls.Button.OnClick()
   at System.Windows.Controls.Primitives.ButtonBase.OnMouseLeftButtonUp(MouseButtonEventArgs e)
   at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target)
   at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
   at System.Windows.UIElement.ReRaiseEventAs(DependencyObject sender, RoutedEventArgs args, RoutedEvent newEvent)
   at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target)
   at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
   at System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args)
   at System.Windows.UIElement.RaiseTrustedEvent(RoutedEventArgs args)
   at System.Windows.Input.InputManager.ProcessStagingArea()
   at System.Windows.Input.InputProviderSite.ReportInput(InputReport inputReport)
   at System.Windows.Interop.HwndMouseInputProvider.ReportInput(IntPtr hwnd, InputMode mode, Int32 timestamp, RawMouseActions actions, Int32 x, Int32 y, Int32 wheel)
   at System.Windows.Interop.HwndMouseInputProvider.FilterMessage(IntPtr hwnd, WindowMessage msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
   at System.Windows.Interop.HwndSource.InputFilterMessage(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 TrailerUnitNewTutor_1.App.Main() in c:\Users\Domnick\Documents\Expression\Blend 4\Projects\TrailerUnitNewTutor_1\TrailerUnitNewTutor_1\obj\Debug\App.g.cs:line 0

1 个答案:

答案 0 :(得分:0)

您的XAML和代码隐藏正在构建两个不同的RenderTransforms。我假设您的XAML故事板中的WrapPanel.RenderTransform看起来像这样:

<WrapPanel.RenderTransform>
    <TransformGroup>
        <ScaleTransform/>
        <SkewTransform/>
        <RotateTransform/>
        <TranslateTransform/>
    </TransformGroup>
</WrapPanel.RenderTransform>

您在代码中创建的RenderTransform在XAML中将如下所示:

<WrapPanel.RenderTransform>
    <TranslateTransform/>
</WrapPanel.RenderTransform>

解决此问题的最简单方法是将XAML中的RenderTransform设置为在代码中设置的方式,即假设您不使用其他变换。如果这不起作用,那么在代码中,创建一个Storyboard,在时间零点设置TranslateTransform。