0

我有一个自定义控件,我想控制其动画。我尝试使用它来更新它,VisualStateManager.GoToState但它总是false在 Silverlight 中返回,并且动画永远不会开始。尽管使用相同的 XAML,但这在 WPF 中完美运行。

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>
4

1 回答 1

0

Put <VisualStateManager.VisualStateGroups> on the first ControlTemplate child :

<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>
                    <!-- ................. -->

Edit : (I full response with your comment) Use VisualStateManager GotoState method with this :

VisualStateManager.GoToState(this, "WorkingState", true);
于 2013-07-19T10:27:10.167 回答