淡入背景图像

Fade in background image

嘿,我正在尝试淡化主 window 上的背景图像。

目前这是我的代码:

10  Dim storyboard__1 As New Storyboard()
20  Dim duration As New TimeSpan(0, 0, 1)
30  Dim animation As New DoubleAnimation()

50  animation.From = 0.0
60  animation.[To] = 1.0
70  animation.Duration = New Duration(duration)

90  Storyboard.SetTargetName(animation, "C:\Users\someone\Downloads\cabd.jpg")
100 Storyboard.SetTargetProperty(animation, New PropertyPath(Control.OpacityProperty))

110 storyboard__1.Children.Add(animation)
120 storyboard__1.Begin(Me.Background)

第 120 行错误 Me.Background。

Error BC30518 Overload resolution failed because no accessible 'Begin' can be called with these arguments: 'Public Overloads Sub Begin(containingObject As FrameworkElement)': Value of type 'Brush' cannot be converted to 'FrameworkElement'. 'Public Overloads Sub Begin(containingObject As FrameworkContentElement)': Value of type 'Brush' cannot be converted to 'FrameworkContentElement'. scrollView

为了调用主窗口上的图像淡入淡出动画,我错过了什么?

您不需要使用故事板。只需在目标 ImageBrush:

上调用 BeginAnimation
Background.BeginAnimation(Brush.OpacityProperty, animation); // C#

请注意,Storyboard.SetTargetName 使用元素的 Name(通常在 XAML 中定义)。设置像 "C:\Users\someone\Downloads\cabd.jpg" 这样的文件路径是没有意义的。


编辑:当然,在尝试为其设置动画之前,您应该将一个可变的 Brush 实例分配给 Background 属性,例如一个 ImageBrush:

var bgBrush = new ImageBrush(new BitmapImage(new Uri(@"C:\Users\someone\Downloads\cabd.jpg")));
bgBrush.BeginAnimation(Brush.OpacityProperty, animation);
Background = bgBrush;