2011-02-16 27 views
2

我在嘗試在Silverlight應用中設置LinearGradientBrush(lgb)的動畫效果。我在頁面的構造函數中有以下代碼:在代碼背後動畫LinearGradientBrush顏色

for (int stops = 0; stops < numStops; stops++) 
    { 
     ColorAnimation animation = new ColorAnimation(); 
     animation.To = 
      Color.FromArgb(255, (byte)rnd.Next(256), (byte)rnd.Next(256), (byte)rnd.Next(256)); 
     animation.Duration = TimeSpan.FromSeconds(1); 
     Storyboard.SetTarget(animation, lgb); 
     Storyboard.SetTargetProperty(animation, 
      new PropertyPath("GradientStops[" + stops.ToString() + "].Color")); 
     Storyboard story = new Storyboard(); 
     story.Children.Add(animation); 
     story.Begin(); 
    } 

它編譯並運行,但不更改顏色。我只是不明白我做錯了什麼。

謝謝
WTS

回答

1

你的代碼的Silverlight 4下和Windows Phone 7的工作正常(WP7是相當多的Silverlight 3)。我的猜測是,如果我爲SL3構建一個獨立的應用程序,它也可以在那裏工作。

您的代碼中唯一遺漏的是lgb是如何獲得的?你確定它是你的UI中實際使用的實例嗎?

例如,我只是說刷到我LayoutRoot電網這樣的: -

<Grid x:Name="LayoutRoot"> 
    <Grid.Background> 
     <LinearGradientBrush EndPoint="1,0.5" StartPoint="0,0.5"> 
      <GradientStop Color="Black" Offset="0" /> 
      <GradientStop Color="White" Offset="1" /> 
     </LinearGradientBrush> 
    </Grid.Background> 

然後在代碼中,我指定用LGB: -

LinearGradientBrush lgb = (LinearGradientBrush)LayoutRoot.Background; 
+0

我會寫一個測試應用程序在3和4中「當我有時間時」,並回到你身邊...... – 2011-02-17 14:13:04