在编程加控制的使用动画故事板 [英] Using a Storyboard animation on a programatically-added control

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

问题描述

我想在一个新的控制现有控件删除后,这是编程添加我的应用程序的应用程序区域褪色。我的code是这样的:

I'm trying to fade in a new control to my application's "app" area which is programatically 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();
	}

然而,当我运行这个code,我得到的错误不适用名称领域存在解析名称'settingsPane'。

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

什么我可能做错了吗?我是pretty肯定,我注册的一切正常(

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

推荐答案

我不会与名称范围等麻烦,宁愿用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();

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

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