简体   繁体   中英

c# Storyboard Begin() Not Firing

I have about 50 <Path> elements on a canvas via a <Viewbox> . The <Viewbox> is also nested. I would like to change the opacity of the <Path> when their locations match sensor data location. The code works all the way up to the Begin() method. It doesn't give an error, it doesn't do anything. I also tried creating different NameScopes but couldn't pin point the correct namspace. Anything other than this does not make it to the Begin() method. If I create a new Scope inside of the if statement: " if (wantedNode is System.Windows.Shapes.Path) " it makes it to Begin() once, doesn't fire, and never comes back!

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)
                .
                .
                .
                .

After all of the angles and magnitudes are covered:

 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);

And finally, the 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>

Try

Storyboard.SetTarget(opacityAnimation, redBrush);

instead of

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

And why dont you just animate the opacity of the element itself?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM