2016-11-04 137 views
0

嘿,我試圖在主窗口背景圖像中淡入淡出。在背景圖片中淡入淡出

目前,這是我的代碼:

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) 

的錯誤是在與Me.Background線120。

錯誤BC30518重載決策失敗,因爲沒有可訪問的「開始」可以用這些參數調用: 「公共重載分開始(containingObject作爲FrameworkElement的)」類型「刷」的值不能轉換爲「FrameworkElement的」 。 'Public Overloads Sub Begin(containsObject As FrameworkContentElement)':類型'Brush'的值不能轉換爲'FrameworkContentElement'。 scrollView

爲了在mainWindow上調用圖像淡化動畫,我錯過了什麼?

回答

2

您不需要使用故事板。只要調用BeginAnimation目標ImageBrush

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

作爲一個說明,Storyboard.SetTargetName使用的元件(通常在XAML定義)的Name。設置像"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; 
+0

我得到的錯誤**嘗試,當上,因爲對象是密封或凍結「System.Windows.Media.SolidColorBrush」不能動畫「不透明度」屬性**。 – StealthRT

+0

因此,您沒有將ImageBrush分配給目標元素的Background?你不是說你想「淡入背景圖像」嗎? – Clemens

+0

是的,但是mainWindow沒有已經可以使用的** Background **屬性? – StealthRT