Using a Storyboard animation on a programmatically-added control

后端 未结 4 1822
不知归路
不知归路 2020-12-13 12:42

I\'m trying to fade in a new control to my application\'s \"app\" area which is programmatically added after the existing controls are removed. My code looks like this:

4条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-13 13:30

    It is possible odd thing but my solution is to use both methods:

    Storyboard.SetTargetName(DA, myObjectName);
    
    Storyboard.SetTarget(DA, myRect);
    
    sb.Begin(this);
    

    In this case there is no error.

    Have a look at the code where I have used it.

     int n = 0;
            bool isWorking;
            Storyboard sb;
            string myObjectName;
             UIElement myElement;
    
            int idx = 0;
    
            void timer_Tick(object sender, EventArgs e)
            {
                if (isWorking == false)
                {
                    isWorking = true;
                    try
                    {
                          myElement = stackObj.Children[idx];
    
                        var possibleIDX = idx + 1;
                        if (possibleIDX == stackObj.Children.Count)
                            idx = 0;
                        else
                            idx++;
    
                        var myRect = (Rectangle)myElement;
    
                       // Debug.WriteLine("TICK: " + myRect.Name);
    
                        var dur = TimeSpan.FromMilliseconds(2000);
    
                        var f = CreateVisibility(dur, myElement, false);
    
                        sb.Children.Add(f);
    
                        Duration d = TimeSpan.FromSeconds(2);
                        DoubleAnimation DA = new DoubleAnimation() { From = 1, To = 0, Duration = d };
    
                        sb.Children.Add(DA);
                        myObjectName = myRect.Name;  
                       Storyboard.SetTargetName(DA, myObjectName);
                       Storyboard.SetTarget(DA, myRect);
    
                        Storyboard.SetTargetProperty(DA, new PropertyPath("Opacity"));
    
                        sb.Begin(this);
    
                        n++;
                    }
                    catch (Exception ex)
                    {
                        Debug.WriteLine(ex.Message + "   " + DateTime.Now.TimeOfDay);
                    }
    
                    isWorking = false;
                }
            }
    

提交回复
热议问题