简体   繁体   English

如何制作 WPF 背景动画循环?

[英]How to make a WPF background animation loop?

I have used this code in MainWindow.xaml (with Storyboards) to make a background animation loop:我在MainWindow.xaml (with Storyboards) 中使用了这段代码来制作背景动画循环:

    <Window.Background>
    <VisualBrush Stretch="UniformToFill" AlignmentX="Left" AlignmentY="Top">
        <VisualBrush.Visual>

            <MediaElement Name="bgv" Opacity="1" IsMuted="True" Width="3840" Height="2160" Stretch="None" HorizontalAlignment="Left" VerticalAlignment="Top">
                <MediaElement.Triggers>
                    <EventTrigger RoutedEvent="MediaElement.Loaded">
                        <EventTrigger.Actions>
                            <BeginStoryboard>
                                <Storyboard>

                                    <MediaTimeline Source="SuperRes/matrixX-animation1.mp4" Storyboard.TargetName="bgv" RepeatBehavior="Forever" />

                                </Storyboard>
                            </BeginStoryboard>
                        </EventTrigger.Actions>
                    </EventTrigger>
                </MediaElement.Triggers>
            </MediaElement>

        </VisualBrush.Visual>
    </VisualBrush>
</Window.Background>

The problem is that the Garbage Collector sometimes stops it from looping or removes it from the window.问题是垃圾收集器有时会阻止它循环或将其从窗口中删除。 Is there another way to make make this video loop?有没有其他方法可以让这个视频循环?

Took me a while to find a solution that works perfectly.我花了一段时间才找到一个完美的解决方案。 Here is the code:这是代码:

XAML: XAML:

<MediaElement x:Name="instructionVideo" Width="320" Height="240" Source="Videos/Instructions.mp4" LoadedBehavior="Manual" Loaded="instructionVideo_Loaded" MediaEnded="instructionVideo_MediaEnded" VerticalAlignment="Bottom" HorizontalAlignment="Center" Canvas.Top="40"/>

CS: CS:

private void instructionVideo_Loaded(object sender, RoutedEventArgs e)
    {
        instructionVideo.Play();
    }
    private void instructionVideo_MediaEnded(object sender, RoutedEventArgs e)
    {
      instructionVideo.Position = TimeSpan.FromSeconds(0);
}

Credits:学分:

http://jcraane.blogspot.com/2012/03/continuously-looping-video-in-wpf.html http://jcraane.blogspot.com/2012/03/continuously-looping-video-in-wpf.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM