更改源后,WPF MediaElement不透明度不会逐渐淡入

时间:2016-02-03 01:47:33

标签: wpf vb.net storyboard

我有一个WPF数字标牌应用程序,可以显示图像和/或视频的幻灯片。我正在使用单个MediaElement对象,并且过渡由Opacity属性的storyboard动画处理。

图像总是平滑淡出,但淡入不会。对于动画持续时间的长度,不透明度似乎保持为0,然后一次性转到1。

Private Function GetFadeInStory() As Storyboard
    Dim story As New Storyboard
    Dim fadeInAnimation As New DoubleAnimation With {
        .BeginTime = New TimeSpan(0),
        .From = 0,
        .To = 1,
        .Duration = TimeSpan.FromMilliseconds(My.Settings.FadeInDuration)}
    Storyboard.SetTargetProperty(fadeInAnimation, New PropertyPath(UIElement.OpacityProperty))

    story.Children.Add(fadeInAnimation)
    Return story
End Function

Private Function GetFadeOutStory() As Storyboard
    Dim story As New Storyboard
    Dim fadeOutAnimation As New DoubleAnimation With {
        .BeginTime = New TimeSpan(0),
        .To = 0,
        .From = 1,
        .Duration = TimeSpan.FromMilliseconds(My.Settings.FadeOutDuration)}
    Storyboard.SetTargetProperty(fadeOutAnimation, New PropertyPath(UIElement.OpacityProperty))

    story.Children.Add(fadeOutAnimation)
    Return story
End Function

Private Sub DisplaySlideImage()
    Dim slideUri As Uri = GetNextSlidePath()
    Dim s As Storyboard = GetFadeOutStory()
    AddHandler s.Completed, Sub() FinishedFadeOut(slideUri)
    Storyboard.SetTarget(s, mediaElement)
    s.Begin()
End Sub

Private Sub FinishedFadeOut(slideUri As Uri)
    mediaElement.Source = slideUri

    Dim s As Storyboard = GetFadeInStory()
    Storyboard.SetTarget(s, mediaElement)
    s.Begin()
End Sub

但是,如果MediaElement.source属性未更改,则淡入和淡出将按预期工作。我尝试过没有成功:

  • 包装BeginInit / EndInit语句中的mediaelement.source更改
  • 更改源
  • 后调用mediaelement.UpateLayout
  • 使用每张幻灯片重新创建mediaelement对象

有什么想法吗?

0 个答案:

没有答案