web-dev-qa-db-fra.com

Utilisation d'une animation Storyboard sur un contrôle ajouté par programme

J'essaie d'intégrer un nouveau contrôle à la zone "application" de mon application, qui est ajoutée par programme après la suppression des contrôles existants. Mon code ressemble à ceci:

        void settingsButton_Clicked(object sender, EventArgs e)
    {
        ContentCanvas.Children.Clear();

        // Fade in settings panel
        NameScope.SetNameScope(this, new NameScope());

        SettingsPane s = new SettingsPane();
        s.Name = "settingsPane";

        this.RegisterName(s.Name, s);
        this.Resources.Add(s.Name, s);

        Storyboard sb = new Storyboard();

        DoubleAnimation settingsFade = new DoubleAnimation();
        settingsFade.From = 0;
        settingsFade.To = 1;
        settingsFade.Duration = new Duration(TimeSpan.FromSeconds(0.33));
        settingsFade.RepeatBehavior = new RepeatBehavior(1);
        Storyboard.SetTargetName(settingsFade, s.Name);
        Storyboard.SetTargetProperty(settingsFade, new PropertyPath(UserControl.OpacityProperty));

        ContentCanvas.Children.Add(s);

        sb.Children.Add(settingsFade);
        sb.Begin();
    }

Cependant, lorsque j'exécute ce code, le message d'erreur "Aucune étendue de nom applicable n'existe pour résoudre le nom 'settingsPane'".

Qu'est-ce que je fais peut-être mal? Je suis sûr que j'ai tout enregistré correctement :(

36
Eric Smith

Je n'aurais pas à me soucier des NameScopes, etc., et utiliserais plutôt Storyboard.SetTarget.

var b = new Button() { Content = "abcd" };
stack.Children.Add(b);

var fade = new DoubleAnimation()
{
    From = 0,
    To = 1,
    Duration = TimeSpan.FromSeconds(5),
};

Storyboard.SetTarget(fade, b);
Storyboard.SetTargetProperty(fade, new PropertyPath(Button.OpacityProperty));

var sb = new Storyboard();
sb.Children.Add(fade);

sb.Begin();
60
user83286

J'ai résolu le problème en utilisant ceci comme paramètre dans la méthode begin, essayez:

sb.Begin(this);

Parce que le nom est enregistré dans la fenêtre.

11
Carlos Angulo

Je suis d'accord, les noms sont probablement la mauvaise chose à utiliser pour ce scénario. Beaucoup plus simple et facile à utiliser SetTarget plutôt que SetTargetName. 

Au cas où cela pourrait aider quelqu'un d'autre, voici ce que j'avais l'habitude de mettre en surbrillance dans une cellule d'un tableau avec une mise en évidence qui disparaît complètement. C'est un peu comme la surbrillance StackOverflow lorsque vous ajoutez une nouvelle réponse. 

    TableCell cell = table.RowGroups[0].Rows[row].Cells[col];

    // The cell contains just one paragraph; it is the first block
    Paragraph p = (Paragraph)cell.Blocks.FirstBlock;

    // Animate the paragraph: fade the background from Yellow to White,
    // once, through a span of 6 seconds.

    SolidColorBrush brush = new SolidColorBrush(Colors.Yellow);
    p.Background = brush;
    ColorAnimation ca1 = new ColorAnimation()
    {
            From = Colors.Yellow,
            To = Colors.White,
            Duration = new Duration(TimeSpan.FromSeconds(6.0)),
            RepeatBehavior = new RepeatBehavior(1),
            AutoReverse = false,
    };

    brush.BeginAnimation(SolidColorBrush.ColorProperty, ca1);
5
Cheeso

C'est une chose étrange mais ma solution consiste à utiliser les deux méthodes:

Storyboard.SetTargetName(DA, myObjectName);

Storyboard.SetTarget(DA, myRect);

sb.Begin(this);

Dans ce cas, il n'y a pas d'erreur.

Regardez le code où je l'ai utilisé.

 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;
            }
        }
0
Academy of Programmer