关于动画切换的若干问题及注意事项

1.通过VisualStateManager触发动画的话,需要将VisualStateManager.VisualStateGroups放在根据容器中
在这里插入图片描述
2.如果有些不属于主界面的动画,尽量在自定义控件中使用,防止在主页面中使用动画不成功。如下:
在主界面中设置
xaml:

<Window x:Class="TestSpotLight.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:TestSpotLight"
        xmlns:sys="clr-namespace:System;assembly=mscorlib"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800" Background="Black">
    <Grid>
        <VisualStateManager.VisualStateGroups>
            <VisualStateGroup >
                <VisualState x:Name="VoiceState">
                    <Storyboard >
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="voice" Storyboard.TargetProperty="(Label.Content)">
                            <DiscreteObjectKeyFrame  KeyTime="0:0:0">
                                <DiscreteObjectKeyFrame.Value>
                                    <sys:String>afsdfasdfasf</sys:String>
                                </DiscreteObjectKeyFrame.Value>
                            </DiscreteObjectKeyFrame>
                        </ObjectAnimationUsingKeyFrames>
                    </Storyboard>
                </VisualState>
                <VisualState Name="normal"/>
            </VisualStateGroup>
        </VisualStateManager.VisualStateGroups>
        <Label Name="voice" Margin="23,9,93,30" Foreground="White"/>
        <Button Content="click" Width="100" Height="30" HorizontalAlignment="Right" Click="Button_Click"/>
    </Grid>
</Window>

C#:

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            var res = VisualStateManager.GoToState(this, "VoiceState", false);
            Console.WriteLine(res);

        }

在这里插入图片描述

在主界面中触发失败,将动画效果写在自定义控件中
xaml:

<UserControl x:Class="TestSpotLight.Button1"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:TestSpotLight"
             xmlns:sys="clr-namespace:System;assembly=mscorlib"
             mc:Ignorable="d" 
             d:DesignHeight="100" d:DesignWidth="200">
    <Grid>
        <VisualStateManager.VisualStateGroups>
            <VisualStateGroup >
                <VisualState x:Name="VoiceState">
                    <Storyboard >
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="voice" Storyboard.TargetProperty="(Label.Content)">
                            <DiscreteObjectKeyFrame  KeyTime="0:0:0">
                                <DiscreteObjectKeyFrame.Value>
                                    <sys:String>afsdfasdfasf</sys:String>
                                </DiscreteObjectKeyFrame.Value>
                            </DiscreteObjectKeyFrame>
                        </ObjectAnimationUsingKeyFrames>
                    </Storyboard>
                </VisualState>
                <VisualState Name="normal"/>
            </VisualStateGroup>
        </VisualStateManager.VisualStateGroups>
        <Label Name="voice" Margin="23,9,93,30" Foreground="White"/>
        <Button Content="click" Width="100" Height="30" HorizontalAlignment="Right" Click="Button_Click"/>
    </Grid>
</UserControl>

C#

 private void Button_Click(object sender, RoutedEventArgs e)
        {
            var res = VisualStateManager.GoToState(this, "VoiceState", false);
            Console.WriteLine(res);
        }

在主界面中调用:

<Window x:Class="TestSpotLight.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:TestSpotLight"
        xmlns:sys="clr-namespace:System;assembly=mscorlib"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800" Background="Black">
    <Grid>
        <local:Button1/>
    </Grid>
</Window>

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值