以编程方式将MediaTimeline与Storyboard中的动画结合 [英] Combining MediaTimeline with animation in Storyboard programmatically

查看:200
本文介绍了以编程方式将MediaTimeline与Storyboard中的动画结合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Storyboard中的MediaTimeline和DoubleAnimation播放带有动画的音频。一切都在没有XAML的C#中实现。在下面的代码中调用Storyboard.Begin之后没有任何反应,也不会引发任何异常。 mediaElement的HasAudio属性为false,并且永远不会调用MediaElement的MediaOpened事件。似乎mediaElement无法加载音频文件。我不知道MediaElement或MediaTimeline是否负责加载音频。没有MediaTimeline,DoubleAnimation可以正常工作。我在这里想念什么?

I am trying to use MediaTimeline and DoubleAnimation inside Storyboard to play audio with animation. Everything is implemented in C# without XAML. Nothing happens after Storyboard.Begin is called in the code below and no exception is thrown. mediaElement's HasAudio property is false and MediaOpened event of MediaElement is never called. It seems mediaElement can't load the audio file. I don't know whether MediaElement or MediaTimeline is responsible for loading the audio. DoubleAnimation works fine without the MediaTimeline. What am I missing here?

private void SetStoryboard(double beginLeft, double endLeft, TimeSpan duration)
{
        mediaElement = new MediaElement();
        mediaElement.LoadedBehavior = MediaState.Manual;
        mediaElement.ScrubbingEnabled = true;
        mediaElement.MediaOpened += new RoutedEventHandler(MediaElement_MediaOpened);

        mediaTimeline = new MediaTimeline();
        mediaTimeline.Duration = new Duration(duration);
        mediaTimeline.Source = new Uri(musicFilePath);
        mediaTimeline.FillBehavior = FillBehavior.Stop;
        Storyboard.SetTarget(mediaTimeline, mediaElement);

        anim = new DoubleAnimation();
        anim.From = beginLeft;
        anim.To = endLeft;
        anim.Duration = new Duration(duration);
        anim.FillBehavior = FillBehavior.Stop;
        Storyboard.SetTarget(anim, slider);
        Storyboard.SetTargetProperty(anim, new PropertyPath(Canvas.LeftProperty));

        storyboard = new Storyboard();
        storyboard.SlipBehavior = SlipBehavior.Slip;
        storyboard.Children.Add(mediaTimeline);
        storyboard.Children.Add(anim);

        storyboard.Begin(this, true);
}


推荐答案

您的MediaElement不属于VisualTree。您正在构造它,但是由于它没有分配给可视化树,因此布局系统将永远不会使用它。

Your MediaElement is not part of the VisualTree. You are constructing it but as it is not assigned to the visual tree it will never get used by the Layout System.

您可以将其添加到代码隐藏中,方法是添加它到一个有效的容器元素。
例如

You can either add it in code-Behind by adding it to a valid container element. e.g.

XAML:

<Grid x:Name="parentGrid">

隐藏代码:

var mediaElement = new MediaElement();
parentGrid.Children.Add(mediaElement);

或者您可以将MediaElement放在页面/窗口上,删除构造MediaElement的行( .. = new MediaElement)并通过以下代码进行访问:

Or you can place the MediaElement on your page/window, remove the line where you construct the MediaElement ( .. = new MediaElement) and just access it via code-behind:

XAML:

<MediaElement x:Name="mediaElement" Width="200" Height="100"/>

这篇关于以编程方式将MediaTimeline与Storyboard中的动画结合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆