用户控制和画布-矢量动画

本文关键字:动画 控制 用户 | 更新日期: 2023-09-27 17:51:05

我想做一个usercontrol,显示和隐藏(可见&倒塌)一个矢量对象在我的项目。为此,我在网格中添加了一个canvas元素。

并尝试制作两个对象Layer1和Layer2的动画。

<UserControl.Resources>
        <Storyboard x:Key="ProgressAnimation" RepeatBehavior="Forever">
                    <ObjectAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="Layer1" Storyboard.TargetProperty="Visibility">
                <ObjectAnimationUsingKeyFrames.KeyFrames>
                    <DiscreteObjectKeyFrame KeyTime="00:00:01" Value="Visible"/>
                </ObjectAnimationUsingKeyFrames.KeyFrames>
            </ObjectAnimationUsingKeyFrames>
            <ObjectAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="Layer2" Storyboard.TargetProperty="Visibility">
                <ObjectAnimationUsingKeyFrames.KeyFrames>
                    <DiscreteObjectKeyFrame KeyTime="00:00:02" Value="Visible"/>
                </ObjectAnimationUsingKeyFrames.KeyFrames>
            </ObjectAnimationUsingKeyFrames>
        </Storyboard>
    </UserControl.Resources> 

接下来我创建一个触发器:

 <UserControl.Triggers>
        <EventTrigger RoutedEvent="FrameworkElement.Loaded">
            <BeginStoryboard x:Name="ProgressAnimationBeginStoryboard" Storyboard="{StaticResource ProgressAnimation}"/>
        </EventTrigger>
    </UserControl.Triggers>

并插入我的元素到Grid:

<Grid>
        <Canvas x:Name="Layer1" Margin="74,112,78,40" Visibility="Collapsed">
            <Path Data="########" Fill="#FF00FF99" Height="148.219" Canvas.Left="0" Canvas.Top="0" Width="147.623"/>
        </Canvas>
        <Canvas x:Name="Layer2" Margin="74,113,79,40" Visibility="Collapsed">
            <Path Data="########" Fill="#FF00FF99" Height="147.36" Canvas.Left="0" Canvas.Top="0" Width="147.243"/>
        </Canvas>
</Grid>

在MainForm中插入代码到XAML:

<Grid>
    <control:LoadingForm HorizontalAlignment="Center" VerticalAlignment="Center"  Height="298" Width="304"/>
</Grid>

并且不能正常工作-得到那个错误:

Error   1   The animation(s) applied to the 'Visibility' property calculate a current value of 'Visible', which is not a valid value for the property.  J:'Projects'LoadingPj'LoadingPj'MainWindow.xaml 1   2   LoadingPj

谁能帮我理解我的问题?谢谢。

用户控制和画布-矢量动画

尝试显式指定Visibility.Visible值:

<DiscreteObjectKeyFrame KeyTime="00:00:01" Value="{x:Static Visibility.Visible}" />

似乎它被当作一个字符串值。