在以编程方式添加的控件上使用 Storyboard 动画 [英] Using a Storyboard animation on a programmatically-added control

查看:35
本文介绍了在以编程方式添加的控件上使用 Storyboard 动画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将新控件淡入到我的应用程序的应用程序"区域,该区域是在删除现有控件后以编程方式添加的.我的代码如下所示:

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:

        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’."

However, when I run this code, I get the error "No applicable name scope exists to resolve the name 'settingsPane'."

我可能做错了什么?我很确定我已经正确注册了所有内容:(

What am I possibly doing wrong? I'm pretty sure I've registered everything properly :(

推荐答案

我不会为 NameScopes 等而烦恼,而是更愿意使用 Storyboard.SetTarget.

I wouldn't hassle with the NameScopes etc. and would rather use Storyboard.SetTarget instead.

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

这篇关于在以编程方式添加的控件上使用 Storyboard 动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆