Canvas中的VisualState故事板在网格内部工作

时间:2018-01-19 13:35:05

标签: canvas uwp grid

我有一个Textblock动画,如果它在画布中,它可以单独运行,但如果我将画布放在Grid中,它就无法工作。它可以使它工作/工作吗?我错过了什么使它成功?

  <Grid Width="3200" Height="99">
    <Canvas Height="70" Width="3200" Margin="-900,0,0,0">
        <VisualStateManager.VisualStateGroups>
            <VisualStateGroup x:Name="States">
                <VisualState x:Name="Left">
                    <Storyboard>
                        <DoubleAnimation Storyboard.TargetName="myTextBlock" Storyboard.TargetProperty="(Canvas.Left)" To="-100" Duration="0:0:2" From="1500" RepeatBehavior="Forever"/>
                    </Storyboard>
                    <VisualState.StateTriggers>
                        <StateTrigger x:Name="desplazamiento" IsActive="true"/>
                    </VisualState.StateTriggers>
                </VisualState>
            </VisualStateGroup>
        </VisualStateManager.VisualStateGroups>
        <!-- Offset the text using a TranslateTransform. -->
        <TextBlock x:Name="myTextBlock"  Text="Text going throught animation" VerticalAlignment="Bottom" Margin="0,-20,0,0" TextWrapping="Wrap" FontSize="33" Canvas.Top="36" />
    </Canvas>
</Grid>

如果删除Grid标签,它确实有效。

1 个答案:

答案 0 :(得分:2)

VisualStateManager只影响内部元素,应该在第一个网格内部控制之后放置。试试这个:

<Grid Width="3200" Height="99">
    <VisualStateManager.VisualStateGroups>
        <VisualStateGroup x:Name="States">
            <VisualState x:Name="Left">
                <Storyboard>
                    <DoubleAnimation Storyboard.TargetName="myTextBlock" Storyboard.TargetProperty="(Canvas.Left)" To="-100" Duration="0:0:2" From="1500" RepeatBehavior="Forever"/>
                </Storyboard>
                <VisualState.StateTriggers>
                    <StateTrigger x:Name="desplazamiento" IsActive="true"/>
                </VisualState.StateTriggers>
            </VisualState>
        </VisualStateGroup>
    </VisualStateManager.VisualStateGroups>

    <Canvas Height="70" Width="3200" Margin="-900,0,0,0">

        <!-- Offset the text using a TranslateTransform. -->
        <TextBlock x:Name="myTextBlock"  Text="Text going throught animation" VerticalAlignment="Bottom" Margin="0,-20,0,0" TextWrapping="Wrap" FontSize="33" Canvas.Top="36" />
    </Canvas>
</Grid>