2010-11-12 26 views
1

我已經創建了一個自定義控件。這是導致問題的方法: -當我反覆按3-4次/秒時出現錯誤。動畫問題

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> 

感謝提前:)

喔!你忘了告訴我,當我點擊按鈕時運行的動畫。

回答

0

在將自定義UserControl添加到網格時,我遇到了類似的問題。

我不是Silverlight中命名範圍的專家,但我認爲這可能是您的問題。在您的控件模板中,您的根網格指定了名稱(x:Name="grid")。當您開始使用此模板動態地將ContentControls添加到grid,並通過grid.Children.Add(newWrapper);行時,名稱不再是唯一的(即您有多個名爲grid的控件,因爲每個newWrapper都使用該名稱的根控件進行模板化)。嘗試將它添加到收藏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); 

之前再次給動態創建ContentControl中對名稱的唯一值(在網格孩子的環境中唯一的),因爲我在命名範圍我不是專家我不確定這會對你的VSM狀態產生什麼樣的影響,StoryBoards將目標放在名爲grid的控件上。至少你會知道這是否是問題。

相關問題