2012-03-05 62 views
0

我是Silverlight的新手,並嘗試通過編程方式爲對象的不透明度創建動畫樣本。Silverlight不透明動畫不能以編程方式工作

我想出用下面的代碼:

 MapShape myTestShape= RadMapInformationLayer.Items[0] as MapShape; 
     SolidColorBrush brush = new SolidColorBrush(); 
     brush.Color = Colors.Purple; 
     myTestShape.Fill = brush; 

     //create a duration object 
     Duration duration = new Duration(TimeSpan.FromSeconds(5)); 

     //create the storyboard 
     Storyboard story = new Storyboard(); 

     //create double animation 
     DoubleAnimation animation = new DoubleAnimation(); 

     //set the duration property 
     animation.Duration = duration; 

     //set the from and too values 
     animation.From = 1.0; 
     animation.To = 0.0; 

     //add the duration to the storyboard 
     story.Duration = duration;    

     //now set the target of the animation 
     Storyboard.SetTarget(animation, myTestShape); 

     //set the target property of this object 
     Storyboard.SetTargetProperty(animation, new PropertyPath(UIElement.OpacityProperty)); 

     //add the double animations to the story board 
     story.Children.Add(animation);    

     if (!LayoutRoot.Resources.Contains("story1")) 
      LayoutRoot.Resources.Add("story1", story); 

     story.Begin(); 

對於屬性路徑我也試過:

1. new PropertyPath("(FrameworkElement.Opacity)") 

2. new PropertyPath("(FrameworkElement.Opacity)") 

3. new PropertyPath("(Control.Opacity)") 

和其他幾個人,我有零運氣這個。

任何人都可以看到我要去哪裏錯了嗎?

感謝, 雅克

+0

我看不出您的代碼有任何問題。當你執行它時會發生什麼?任何錯誤?異常? – ColinE 2012-03-05 16:59:47

+0

代碼適用於矩形。也許這是動畫MapShape的一個問題。 – foson 2012-03-05 21:53:43

+0

感謝您的回覆Foson和ColinE ColinE:沒有錯誤,它只是沒有做任何事情 Foson:它可能是MapShape對象,不知道它是否會改變您的想法,但它從Control對象繼承? – Jacques 2012-03-06 12:52:27

回答

1

我以前做數據綁定到MapShape.FillProperty。

嘗試:

 //create double animation 
     ColorAnimation animation = new ColorAnimation(); 

     //set the duration property 
     animation.Duration = duration; 

     //set the from and too values 
     animation.From = Colors.Purple; 
     animation.To = Colors.Transparent; 

     //add the duration to the storyboard 
     story.Duration = duration; 

     //now set the target of the animation 
     Storyboard.SetTarget(animation, rect); 

     //set the target property of this object 
     Storyboard.SetTargetProperty(animation, new PropertyPath("(MapShape.Fill).Color")); 

編輯:

至於爲什麼你的代碼不能正常工作 - 通過MapShape.SetShapeFillStroke()來看,似乎Telerik的將不綁定MapShape的不透明度到它的內在原始形狀的不透明度,除非你提供填充。你有在XAML中定義的填充嗎?如果沒有,請嘗試提供一個。否則,也許這個代碼在Shape的生命週期中(在ReadCompleted中或之後)過早定義?

<telerik:InformationLayer x:Name="StateLayer"> 
    <telerik:InformationLayer.Reader> 
     ... 
    </telerik:InformationLayer.Reader> 
    <telerik:InformationLayer.ShapeFill> 
     <telerik:MapShapeFill Fill="{StaticResource CommonBackgroundLightBrush}" Stroke="#5A636B" StrokeThickness="1" /> 
    </telerik:InformationLayer.ShapeFill> 
+0

嗨福森,我會盡快給你一個嘗試。任何原因爲什麼我的不透明代碼不起作用? – Jacques 2012-03-06 12:52:50

+0

嗨Foson,我已經試過你的代碼,並且工作的是地圖形狀變成透明的,但它絕對不是隨時間變化的動畫,它是瞬時的。 TimeSpan.FromSeconds(5)...這意味着5秒的權利? – Jacques 2012-03-07 12:30:04

+0

更正:它根本沒有工作,我實際上沒有放在PropertyPath的.Color部分。當我添加它時,我得到一個異常:無法解析指定對象上的TargetProperty(MapShape.Fill).Color。 – Jacques 2012-03-07 12:52:09