프로그래밍 방식으로 컨트롤에 스토리 보드 애니메이션을 사용합니다

StackOverflow https://stackoverflow.com/questions/695782

  •  22-08-2019
  •  | 
  •  

문제

기존 컨트롤이 제거 된 후 프로그래밍 방식으로 추가되는 애플리케이션의 "앱"영역에 대한 새로운 컨트롤로 페이드하려고합니다. 내 코드는 다음과 같습니다.

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

그러나이 코드를 실행하면 " 'settingspane'이라는 이름을 해결하기 위해 적용 가능한 이름 범위가 없습니다."오류가 발생합니다.

내가 뭘 잘못하고 있을까요? 나는 모든 것을 올바르게 등록했다고 확신합니다 :(

도움이 되었습니까?

해결책

나는 Namescopes 등에 번거롭지 않을 것이며 대신 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();

다른 팁

이것을 시작 메소드에서 매개 변수로 사용하여 문제를 해결했습니다.

sb.Begin(this);

이름이 창에 등록되어 있기 때문입니다.

나는 Namescopes가 아마도이 시나리오에 사용하기에 잘못된 것일 것입니다. SettArgetName보다는 SettArget을 사용하기가 훨씬 간단하고 쉽습니다.

다른 사람을 돕는 경우, 여기에 내가 아무것도 쇠퇴하는 하이라이트가있는 테이블에서 특정 셀을 강조하는 데 사용한 내용이 있습니다. 새로운 답변을 추가 할 때 StackoverFlow 하이라이트와 비슷합니다.

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

홀수는 가능하지만 내 해결책은 두 가지 방법을 모두 사용하는 것입니다.

Storyboard.SetTargetName(DA, myObjectName);

Storyboard.SetTarget(DA, myRect);

sb.Begin(this);

이 경우 오류가 없습니다.

내가 사용한 코드를 살펴보십시오.

 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;
            }
        }
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top