ExtendedVisualStateManager.GoToElementState在Silverlight中返回fa

本文关键字:返回 fa Silverlight GoToElementState ExtendedVisualStateManager | 更新日期: 2023-09-27 18:23:58

我有一个自定义控件,我想控制它的动画。我尝试使用VisualStateManager.GoToState更新它,但它总是在Silverlight中返回false,并且动画从未开始。这在WPF中非常有效,尽管使用相同的XAML。

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>

ExtendedVisualStateManager.GoToElementState在Silverlight中返回fa

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

编辑:(我完全回应您的评论)使用VisualStateManager GotoState方法和this:

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