2010-02-12 93 views
14

我不知道是否有人可以幫助我 - 我有一個標籤,當需要在後面的代碼中調用一個方法時,我需要能夠在任何兩種顏色之間進行交叉淡入淡出。WPF ColorAnimation刷機屬性

我迄今爲止最好的嘗試:

Private OldColor as Color = Colors.White 
Sub SetPulseColor(ByVal NewColor As Color) 
    Dim F As New Animation.ColorAnimation(OldColor, NewColor, New Duration(TimeSpan.Parse("00:00:01"))) 
    OldColor = NewColor 
    F.AutoReverse = False 
    PulseLogo.BeginAnimation(Label.ForegroundProperty, F) 

End Sub 

我的問題是,ColorAnimation返回Media.Color和物業類型前景刷。

我知道如何創建合適的畫筆,但不知道如何在動畫中做到這一點。

從谷歌搜索,看來我需要一個轉換器:

<ValueConversion(GetType(SolidColorBrush), GetType(SolidColorBrush))> _ 
Public Class ColorConverter 
    Implements IValueConverter 

Public Function Convert(ByVal value As Object, ByVal targetType As Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) As Object Implements System.Windows.Data.IValueConverter.Convert 
     Dim Color As Color = DirectCast(value, Color) 
     Return New SolidColorBrush(Color) 
    End Function 

    Public Function ConvertBack(ByVal value As Object, ByVal targetType As Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) As Object Implements System.Windows.Data.IValueConverter.ConvertBack 
     Return Nothing 
    End Function 

End Class 

,但我已經看到了將其綁定到動畫中的XAML的所有例子 - 我想做到這一點在後面的代碼。 ..

有人能指點我在正確的方向嗎?

感謝

回答

21

通常的解決方案是不使用轉換器,而是以動畫畫筆的顏色。然而,要做到這一點,你需要的PropertyPath,這反過來又意味着你需要一個故事板:

Storyboard s = new Storyboard(); 
s.Duration = new Duration(new TimeSpan(0, 0, 1)); 
s.Children.Add(F); 

Storyboard.SetTarget(F, PulseLogo); 
Storyboard.SetTargetProperty(F, new PropertyPath("Foreground.Color")); 

s.Begin(); 

(原諒C#語法)

注意在SetTargetProperty調用屬性路徑,它通過前景將遍歷屬性以及生成的畫筆的顏色屬性。

也可以使用該技術進行動畫各個梯度停止在漸變畫筆等

+0

這真的很高雅 - 我現在就試試。 [編輯]錯誤:無法爲'System.Windows.Media.SolidColorBrush'上的'Color'屬性設置動畫,因爲對象被封閉或凍結。 [我的代碼]:\t \t昏暗BR作爲的SolidColorBrush = DirectCast((PulseLogo.Foreground)的SolidColorBrush) \t \t PulseLogo.Foreground.BeginAnimation(SolidColorBrush.ColorProperty,F) 「感謝您的幫助 – Basic 2010-02-12 01:09:57

+0

對不起,夥計,我弄亂。我以前只用故事板做過,我天真地認爲我可以直接將它翻譯成BeginAnimation調用,這是錯誤的。我已經更新了答案,現在用實際的\ * gasp \ *誠實善良的測試代碼 - 希望這對你更好。 – itowlson 2010-02-12 01:40:27

+0

C#不用擔心 - 它們非常相似,只不過是一種方言:)感謝您提供更新的解決方案 - 它的工作原理完美無瑕。 – Basic 2010-02-13 20:35:31

0
  ColorAnimation colorChangeAnimation = new ColorAnimation(); 
      colorChangeAnimation.From = VariableColour; 
      colorChangeAnimation.To = BaseColour; 
      colorChangeAnimation.Duration = timeSpan; 

      PropertyPath colorTargetPath = new PropertyPath("(Panel.Background).(SolidColorBrush.Color)"); 
      Storyboard CellBackgroundChangeStory = new Storyboard(); 
      Storyboard.SetTarget(colorChangeAnimation, BackGroundCellGrid); 
      Storyboard.SetTargetProperty(colorChangeAnimation, colorTargetPath); 
      CellBackgroundChangeStory.Children.Add(colorChangeAnimation); 
      CellBackgroundChangeStory.Begin(); 

// VariableColour & BaseColour是類別Color的,時間跨度是時間跨度的類,BackGroundCellGrid是類網格的;

//無需創建SolidColorBrush並在XAML中對其進行綁定; //玩得開心!