动画未在使用DispatcherTimer的Windows 8.1应用程序中运行

本文关键字:应用程序 运行 Windows DispatcherTimer 动画 | 更新日期: 2023-09-27 17:58:35

我有一个简单的Storyboard,它将网格旋转到60度,然后返回到0,但动画没有运行,我不知道为什么。我使用DispatcherTimer来确保它以设定的间隔运行。我还尝试在xaml中创建Storyboard,并从Ticker事件中调用Storyboard.Begin(),但没有成功。提前谢谢。调试时,我在Ticker事件中添加了一个断点,动画运行

主页.cs

public sealed partial class MainPage : Page
{
    private DispatcherTimer timer;
    public MainPage()
    {
        this.InitializeComponent();
        Loaded += (s, args) =>
        {
            timer = new DispatcherTimer();
            timer.Tick += Ticker;
            timer.Interval = new TimeSpan(0, 0, 6);
            timer.Start();                
        };
    }
    public void Ticker(object sender, object e)
    {
        var sb = new Storyboard();
        var animation2 = new DoubleAnimationUsingKeyFrames();
        var ease0 = new EasingDoubleKeyFrame() { KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromMilliseconds(0)), Value = 0 };
        var ease1 = new EasingDoubleKeyFrame() { KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromMilliseconds(2500)), Value = 40 };
        var ease2 = new EasingDoubleKeyFrame() { KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromMilliseconds(4000)), Value = 60 };
        var ease3 = new EasingDoubleKeyFrame() { KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromMilliseconds(4100)), Value = 0 };
        animation2.KeyFrames.Add(ease0);
        animation2.KeyFrames.Add(ease1);
        animation2.KeyFrames.Add(ease2);
        animation2.KeyFrames.Add(ease3);
        Storyboard.SetTargetProperty(animation2, "(UIElement.Projection).(PlaneProjection.RotationY)");
        Storyboard.SetTarget(animation2, grid2);
        sb.Children.Add(animation2);
        sb.Completed += (se, argss) =>
        {
        };
        sb.Begin();            
    }

主页.xaml

 <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">       
        <Grid x:Name="grid2" Width="250" Height="250" Background="Blue" Grid.Column="1">
            <Grid.Projection>
                <PlaneProjection/>
            </Grid.Projection>
        </Grid>      
</Grid>

动画未在使用DispatcherTimer的Windows 8.1应用程序中运行

希望这能有所帮助。

可以使用BeginTime和Duration设置动画的时间轴。

我使用了begintime而不是timer。下面的代码在这里运行良好。

  protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        var sb = new Storyboard();
        var animation2 = new DoubleAnimationUsingKeyFrames();
        animation2.BeginTime = TimeSpan.FromMilliseconds(6);
        var ease0 = new EasingDoubleKeyFrame() { KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromMilliseconds(0)), Value = 0 };
        var ease1 = new EasingDoubleKeyFrame() { KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromMilliseconds(2500)), Value = 40 };
        var ease2 = new EasingDoubleKeyFrame() { KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromMilliseconds(4000)), Value = 60 };
        var ease3 = new EasingDoubleKeyFrame() { KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromMilliseconds(4100)), Value = 0 };
        animation2.KeyFrames.Add(ease0);
        animation2.KeyFrames.Add(ease1);
        animation2.KeyFrames.Add(ease2);
        animation2.KeyFrames.Add(ease3);
        Storyboard.SetTargetProperty(animation2, "(UIElement.Projection).(PlaneProjection.RotationY)");
        Storyboard.SetTarget(animation2, grid2);
        sb.Children.Add(animation2);
        sb.Completed += (se, argss) =>
        {
        };
        sb.Begin();
    }

使用xaml也可以实现相同的动画。

 <Grid Background="Black">
    <Grid x:Name="grid2" Width="250" Height="250" Background="Blue" Grid.Column="1">
        <Grid.Projection>
            <PlaneProjection/>
        </Grid.Projection>
        <Grid.Triggers>
            <EventTrigger>
                <BeginStoryboard>
                    <Storyboard>
                        <DoubleAnimationUsingKeyFrames EnableDependentAnimation="True" Storyboard.TargetProperty="(UIElement.Projection).(PlaneProjection.RotationY)" Storyboard.TargetName="grid2">
                            <EasingDoubleKeyFrame KeyTime="0:0:0.1" Value="0"/>
                            <EasingDoubleKeyFrame KeyTime="0:0:0.7" Value="40"/>
                            <EasingDoubleKeyFrame KeyTime="0:0:0.9" Value="60"/>
                            <EasingDoubleKeyFrame KeyTime="0:0:0.15" Value="0"/>
                        </DoubleAnimationUsingKeyFrames>
                    </Storyboard>
                </BeginStoryboard>
            </EventTrigger>
        </Grid.Triggers>
    </Grid>
</Grid>