2013-07-19 92 views
0

我有一個自定義控件,其動畫我想要控制。我嘗試使用VisualStateManager.GoToState來更新它,但它始終在Silverlight中返回false,並且動畫永遠不會啓動。儘管使用相同的XAML,但它在WPF中完美工作。ExtendedVisualStateManager.GoToElementState在Silverlight中返回false

Silverlight的代碼

//to start 
retval = ExtendedVisualStateManager.GoToElementState(this.canvasParent, "WorkingState", true); 

的WPF代碼

retval = VisualStateManager.GoToElementState(this.canvasParent, "WorkingState", true); 

XAML(通用):

<Style TargetType="local:WaitSpinner"> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="local:WaitSpinner"> 
       <Viewbox Visibility="{TemplateBinding Visibility}"> 
        <Canvas RenderTransformOrigin="0.5,0.5" x:Name="CanvasParent" Width="120" Height="120"> 
         <!-- other awesomeness --> 

         <VisualStateManager.VisualStateGroups> 
          <VisualStateGroup x:Name="Working"> 
           <VisualState x:Name="WorkingState"> 
            <Storyboard> 
             <DoubleAnimation 
             Storyboard.TargetName="SpinnerRotate" 
             Storyboard.TargetProperty="Angle" 
             From="0" To="360" Duration="0:0:01.3" 
             RepeatBehavior="Forever" /> 
            </Storyboard> 
           </VisualState> 
           <VisualState x:Name="Stop"/> 
          </VisualStateGroup> 
         </VisualStateManager.VisualStateGroups> 
        </Canvas> 
       </Viewbox> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 

回答

0

<VisualStateManager.VisualStateGroups>第一ControlTemplate孩子:

<Style TargetType="local:WaitSpinner"> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="local:WaitSpinner"> 
       <Viewbox Visibility="{TemplateBinding Visibility}"> 
        <VisualStateManager.VisualStateGroups> 
         <VisualStateGroup x:Name="Working"> 
          <VisualState x:Name="WorkingState"> 
           <Storyboard> 
            <DoubleAnimation 
            Storyboard.TargetName="SpinnerRotate" 
            Storyboard.TargetProperty="Angle" 
            From="0" To="360" Duration="0:0:01.3" 
            RepeatBehavior="Forever" /> 
           </Storyboard> 
          </VisualState> 
          <VisualState x:Name="Stop"/> 
         </VisualStateGroup> 
        </VisualStateManager.VisualStateGroups> 
        <!-- ................. --> 

編輯:(我與您的評論完全反應)使用VisualStateManagerGotoStatethis

VisualStateManager.GoToState(this, "WorkingState", true); 
+0

,不幸的是沒有什麼區別 – Chris

+0

修正:當使用'VisualStateManager.GotoState'用'this'它的工作原理!我只使用擴展的,因爲這個起源沒有工作,謝謝 – Chris

+0

是的,我沒有看到'this.canvasParent'和'GoToElementState'抱歉...我將它添加到我的答案... – Tonio