在wpf

时间:2015-11-10 18:21:36

标签: c# wpf storyboard

我想在故事板中播放两次相同的声音。 不知怎的,这不起作用,只播放一次声音(5秒后):

    public ViewModel()
    {
        Storyboard sb = new Storyboard();
        MediaElementObject = new MediaElement();

        MediaTimeline mtl = new MediaTimeline(new Uri(@"./Resources/Ka-ching.Wav", UriKind.RelativeOrAbsolute));

        mtl.BeginTime = TimeSpan.FromSeconds(0.3);
        Storyboard.SetTarget(mtl, MediaElementObject);
        sb.Children.Add(mtl);
        mtl.BeginTime = TimeSpan.FromSeconds(5);
        Storyboard.SetTarget(mtl, MediaElementObject);
        sb.Children.Add(mtl);
        sb.Begin();
    }

故事板有两个孩子,但两个孩子的开始时间设置为5秒。我也试过这个(在实际情况下会更难实现,但它也不起作用):

    public ViewModel()
    {
        Storyboard sb = new Storyboard();
        MediaElementObject = new MediaElement();

        MediaTimeline mtl = new MediaTimeline(new Uri(@"./Resources/Ka-ching.Wav", UriKind.RelativeOrAbsolute));
        MediaTimeline mtl2 = new MediaTimeline(new Uri(@"./Resources/Ka-ching.Wav", UriKind.RelativeOrAbsolute));

        mtl.BeginTime = TimeSpan.FromSeconds(0.3);
        Storyboard.SetTarget(mtl, MediaElementObject);
        sb.Children.Add(mtl);
        mtl2.BeginTime = TimeSpan.FromSeconds(5);
        Storyboard.SetTarget(mtl2, MediaElementObject);
        sb.Children.Add(mtl2);
        sb.Begin();
    }

现在,孩子们有两个不同的开始时间(在调试器中),但它仍然只播放一次声音。

0 个答案:

没有答案