Silverlight 当我每秒重复按下按钮3-4次时,会出现错误。动画问题

Silverlight 当我每秒重复按下按钮3-4次时,会出现错误。动画问题,silverlight,animation,silverlight-4.0,Silverlight,Animation,Silverlight 4.0,我已经创建了一个自定义控件。这是导致问题的方法:- private void Update(object newContent) { //lock (this) //{ if (grid == null) base.ApplyTemplate(); ContentControl oldWrapper = this.current

我已经创建了一个自定义控件。这是导致问题的方法:-

private void Update(object newContent)
        {
            //lock (this)
            //{
                if (grid == null)
                    base.ApplyTemplate();

                ContentControl oldWrapper = this.currentWrappedControl;
                if (oldWrapper != null)
                {
                    VisualStateGroup layoutStatesGroup = findNameInWrapper(oldWrapper, "LayoutStates") as VisualStateGroup;

                    if (layoutStatesGroup == null)
                    {
                        this.grid.Children.Remove(oldWrapper);
                        setContent(oldWrapper, null);
                    }
                    else
                    {
                        layoutStatesGroup.CurrentStateChanged += delegate(object sender, VisualStateChangedEventArgs args)
                        {

                            this.grid.Children.Remove(oldWrapper);
                            setContent(oldWrapper, null);
                        };
                        VisualStateManager.GoToState(oldWrapper, "BeforeUnloaded", true);
                    }
                }

                ContentControl newWrapper = new ContentControl();
                newWrapper.Style = TransitionStyle;
                newWrapper.HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch;
                newWrapper.VerticalAlignment = System.Windows.VerticalAlignment.Stretch;
                grid.Children.Add(newWrapper);
                newWrapper.ApplyTemplate();
                if (this.TransitionStyle != null)
                {
                    setContent(newWrapper, newContent);
                    if (oldWrapper != null)
                    {
                        VisualStateManager.GoToState(newWrapper, "BeforeLoaded", false);
                        VisualStateManager.GoToState(newWrapper, "AfterLoaded", true);
                    }
                }
                this.currentWrappedControl = newWrapper;
            //}
        }
如果我慢慢按动按钮,一切都很顺利。但如果我不安地按下按钮,它就会崩溃,我会得到这个错误:-

Microsoft JScript runtime error: Unhandled Error in Silverlight Application Value does not fall within the expected range.   at MS.Internal.XcpImports.CheckHResult(UInt32 hr)
   at MS.Internal.XcpImports.SetValue(IManagedPeerBase obj, DependencyProperty property, DependencyObject doh)
   at MS.Internal.XcpImports.SetValue(IManagedPeerBase doh, DependencyProperty property, Object obj)
   at System.Windows.DependencyObject.SetObjectValueToCore(DependencyProperty dp, Object value)
   at System.Windows.DependencyObject.SetEffectiveValue(DependencyProperty property, EffectiveValueEntry& newEntry, Object newValue)
   at System.Windows.DependencyObject.UpdateEffectiveValue(DependencyProperty property, EffectiveValueEntry oldEntry, EffectiveValueEntry& newEntry, ValueOperation operation)
   at System.Windows.DependencyObject.SetValueInternal(DependencyProperty dp, Object value, Boolean allowReadOnlySet)
   at System.Windows.Controls.ContentPresenter.set_Content(Object value)
   at SilverlightTransitionEffect.TransitionalControl.setContent(ContentControl control, Object Content)
   at SilverlightTransitionEffect.TransitionalControl.Update(Object newContent)
   at SilverlightTransitionEffect.TransitionalControl.OnContentChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
   at System.Windows.DependencyObject.RaisePropertyChangeNotifications(DependencyProperty dp, Object oldValue, Object newValue)
   at System.Windows.DependencyObject.UpdateEffectiveValue(DependencyProperty property, EffectiveValueEntry oldEntry, EffectiveValueEntry& newEntry, ValueOperation operation)
   at System.Windows.DependencyObject.RefreshExpression(DependencyProperty dp)
   at System.Windows.DependencyPropertyChangedWeakListener.SourcePropertyChanged(DependencyObject c, DependencyProperty dp)
   at System.Windows.DependencyObject.OnPropertyChanged(DependencyProperty dp)
   at System.Windows.FrameworkElement.OnPropertyChanged(DependencyProperty dp)
   at System.Windows.DependencyObject.RaisePropertyChangeNotifications(DependencyProperty dp, Object oldValue, Object newValue)
   at System.Windows.DependencyObject.UpdateEffectiveValue(DependencyProperty property, EffectiveValueEntry oldEntry, EffectiveValueEntry& newEntry, ValueOperation operation)
   at System.Windows.DependencyObject.SetValueInternal(DependencyProperty dp, Object value, Boolean allowReadOnlySet)
   at System.Windows.DependencyObject.SetValue(DependencyProperty dp, Object value)
   at SilverlightTransitionEffect.Slideshow.set_SelectedItem(Object value)
   at SilverlightTransitionEffect.Slideshow.btnRightButton_Click(Object sender, RoutedEventArgs e)
   at System.Windows.Controls.Primitives.ButtonBase.OnClick()
   at System.Windows.Controls.Button.OnClick()
   at System.Windows.Controls.Primitives.ButtonBase.OnMouseLeftButtonUp(MouseButtonEventArgs e)
   at System.Windows.Controls.Control.OnMouseLeftButtonUp(Control ctrl, EventArgs e)
   at MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj, IntPtr unmanagedObjArgs, Int32 argsTypeIndex, Int32 actualArgsTypeIndex, String eventName)
我甚至尝试锁定对象,但仍然存在相同的问题

这是我的视觉状态:-

 <Style x:Key="SlideTemplate" TargetType="ContentControl">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="ContentControl">
                    <Grid x:Name="grid" Background="{TemplateBinding Background}">
                        <VisualStateManager.VisualStateGroups>
                            <VisualStateGroup x:Name="LayoutStates">
                                <VisualStateGroup.Transitions>
                                    <VisualTransition GeneratedDuration="0:0:0.5">
                                        <VisualTransition.GeneratedEasingFunction>
                                            <CubicEase EasingMode="EaseOut"/>
                                        </VisualTransition.GeneratedEasingFunction>
                                    </VisualTransition>
                                </VisualStateGroup.Transitions>
                                <VisualState x:Name="BeforeLoaded">
                                    <Storyboard >
                                        <DoubleAnimation Duration="0" To="500" Storyboard.TargetProperty="(UIElement.RenderTransform).(CompositeTransform.TranslateX)" Storyboard.TargetName="grid"/>
                                        <DoubleAnimation Duration="0" To="0" Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="grid"/>
                                    </Storyboard>
                                </VisualState>
                                <VisualState x:Name="AfterLoaded"/>
                                <VisualState x:Name="BeforeUnloaded">
                                    <Storyboard>
                                        <DoubleAnimation Duration="0" To="-1000" Storyboard.TargetProperty="(UIElement.RenderTransform).(CompositeTransform.TranslateX)" Storyboard.TargetName="grid"/>
                                        <DoubleAnimation Duration="0" To="0" Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="grid"/>
                                    </Storyboard>
                                </VisualState>
                            </VisualStateGroup>
                        </VisualStateManager.VisualStateGroups>
                        <Grid.RenderTransform>
                            <CompositeTransform/>
                        </Grid.RenderTransform>

                        <ContentPresenter x:Name="contentPresenter"  Content="{TemplateBinding Content}" />

                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

提前感谢:)


哦!是的,我忘了告诉你当我点击按钮时动画会运行。

我在向网格添加自定义用户控件时遇到了类似的问题

我不是Silverlight中命名范围的专家,但我认为这可能是您的问题。在控件模板中,指定了名称为(
x:name=“grid”
)的根网格。当您开始使用此模板通过行
grid.Children.Add(newWrapper)向
grid
动态添加ContentControls时名称不再是唯一的(即,您有多个名为grid的控件,因为每个newWrapper都使用该名称的根控件作为模板)。在将动态创建的ContentControl添加到Grid.children集合(即

ContentControl newWrapper = new ContentControl();

newWrapper.Name = "someUniqueName"; //ensure this is uniquely generated on each call

newWrapper.Style = TransitionStyle;
newWrapper.HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch;
newWrapper.VerticalAlignment = System.Windows.VerticalAlignment.Stretch;
grid.Children.Add(newWrapper);
同样,因为我不是命名作用域的专家,所以我不确定这会对VSM状态产生什么影响,在VSM状态中,故事板以名为grid的控件为目标。但至少你会知道这是否是问题所在