2013-09-24 30 views
0

我在畫布上通過<Viewbox>約有50 <Path>個元素。 <Viewbox>也是嵌套的。當他們的位置匹配傳感器數據位置時,我想更改<Path>的不透明度。該代碼一直運行到Begin()方法。它不會給出錯誤,它不會做任何事情。我也嘗試創建不同的NameScopes,但無法指出正確的namspace。除了this之外的任何內容都不會使其成爲Begin()方法。如果我在if語句中創建了一個新的Scope:「if (wantedNode is System.Windows.Shapes.Path)」,它會將其設置爲Begin()一次,不會觸發,並且永不再回來!c#Storyboard Begin()Not Firing

if (angle > 0) 
     { 
      if (angle <= 180 && angle > 150) 
      { 
       if (mag <= sweetSpot) 
       { 
        //posAlertTxt.Text = "On target..."; 
       } 
       else if (mag <= step) 
       { 
        selectedStep = "step0108"; 
       } 
       else if (mag <= step * 2) 
       . 
       . 
       . 
       . 

所有的角度和大小後覆蓋:

redBrush.Color = Colors.IndianRed; 

      System.Windows.Media.Animation.DoubleAnimation opacityAnimation = new System.Windows.Media.Animation.DoubleAnimation(); 

      opacityAnimation.To = 1.0; 
      opacityAnimation.Duration = TimeSpan.FromSeconds(1); 
      opacityAnimation.AutoReverse = true; 

      RegisterName("redBrush", redBrush); 

      Storyboard sb = new System.Windows.Media.Animation.Storyboard(); 

      Storyboard.SetTargetName(opacityAnimation, "redBrush"); 
      Storyboard.SetTargetProperty(opacityAnimation, new PropertyPath(SolidColorBrush.OpacityProperty)); 

      sb.Children.Add(opacityAnimation); 

      object wantedNode = indicatorBounds.FindName(selectedStep); 
      if (wantedNode is System.Windows.Shapes.Path){ 
       System.Windows.Shapes.Path wantedChild = wantedNode as System.Windows.Shapes.Path; 
       wantedChild.Fill = redBrush; 
       //System.Diagnostics.Debug.WriteLine(wantedChild.Opacity);   
       sb.Begin(this); 

最後,XAML:

. 
. 
. 
. 
<Viewbox Name="forceGradientContainer" Width="126" Height="457" 
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Canvas.Left="117" Canvas.Top="17"> 
    <Canvas Name="indicatorBounds" Width="126.378" Height="457.004"> 
     <Canvas Name="holder"> 
      . 
      . 
      . 
      . 
      <Path Fill="IndianRed" Opacity="0" x:Name="step0108" Data="F1 M 28.802,209.027 L  49.252,221.067 C 47.922,223.377 47.162,226.057 47.162,228.917 L 23.432,228.917 C 23.432,221.667 25.392,214.867 28.802,209.027 Z"/>` 
      . 
      . 
      . 
     </Canvas>  
    </Canvas> 
</Viewbox> 

回答

0

嘗試

Storyboard.SetTarget(opacityAnimation, redBrush); 

,而不是

RegisterName("redBrush", redBrush); 
Storyboard.SetTargetName(opacityAnimation, "redBrush"); 

爲什麼不只是動畫元素本身的不透明度?

Storyboard.SetTarget(opacityAnimation, wantedChild); 
Storyboard.SetTargetProperty(opacityAnimation, new PropertyPath("Opacity")); 
+0

這樣做。謝謝!它看起來像AutoReverse參數無法正常工作。它必須是因爲當傳感器移動時(在前面的呼叫可以淡出之前)再次調用該功能。無論如何要泡這個? – atomSmasher

+0

嘗試在Storyboard實例中設置AutoReverse屬性:'sb.AutoReverse = true;'。也許opacityAnimation在添加到'sb'時採用AutoReverse的值,默認爲false。 –

+0

不行。在第一次開始之後,我最終創建了另一個動畫,將不透明度設置爲0.它仍然有點麻煩,但做得更好。 – atomSmasher