StealthRT StealthRT - 1 year ago 103 Question

Fade in background image

Hey all I am trying to fade in a background image on the main window.

Currently this is my code:

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)

The error is on line 120 with the 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

What am I missing in order to call the image fade animation on the mainWindow?

Answer Source

You don't need to use a Storyboard. Just call BeginAnimation on the target ImageBrush:

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

As a note, Storyboard.SetTargetName uses the Name of an element (usually defined in XAML). Setting a file path like "C:\Users\someone\Downloads\cabd.jpg" is pointless.

EDIT: You should of course assign a mutable Brush instance to the Background property before trying to animate it, e.g. an ImageBrush:

var bgBrush = new ImageBrush(new BitmapImage(new Uri(@"C:\Users\someone\Downloads\cabd.jpg")));
bgBrush.BeginAnimation(Brush.OpacityProperty, animation);
Background = bgBrush;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download