C# 如何以编程方式在UWP中使用FadeInAnimation.class?
我正在尝试以编程方式进行一个简单的淡入动画。 在XAML中,我有一个包含命名网格的页面 后面的代码如下所示:C# 如何以编程方式在UWP中使用FadeInAnimation.class?,c#,.net,uwp,fadein,C#,.net,Uwp,Fadein,我正在尝试以编程方式进行一个简单的淡入动画。 在XAML中,我有一个包含命名网格的页面 后面的代码如下所示: InitializeComponent(); Button button = new Button() { Width = 200, Height = 200, Content = "Text", Background = new SolidColorBrush(Colors.Red) }; button.Name = "target"; Storybo
InitializeComponent();
Button button = new Button() {
Width = 200,
Height = 200,
Content = "Text",
Background = new SolidColorBrush(Colors.Red)
};
button.Name = "target";
Storyboard myStory = new Storyboard();
FadeInThemeAnimation animation = new FadeInThemeAnimation() {
BeginTime = new TimeSpan(2),
Duration = new TimeSpan(4),
};
animation.TargetName = button.Name;
Storyboard.SetTarget(animation, button);
Storyboard.SetTargetProperty(animation, button.Name);
myStory.Children.Add(animation);
myGrid.Children.Add(button);
button.PointerEntered += (s, e) => {
myStory.Begin();
};
Storyboard myStory = new Storyboard();
FadeOutThemeAnimation animation = new FadeOutThemeAnimation()
{
Duration = TimeSpan.FromMilliseconds(500)
};
Storyboard.SetTarget(animation, button);
myStory.Children.Add(animation);
PlayGround.PointerPressed += (kk, kkk) => myStory.Begin();
问题是我没有看到任何动画。另外,我不确定在哪里调用begin()
。最好放在加载的事件中还是放在其他地方
编辑:是我尝试使用的FadeInTheme动画。您的代码存在许多问题
首先,在官方文件中,FadeInThemeAnimation
被描述为
预配置不透明度动画
因此,它应该将ui元素的Opacity
增加到1.0或类似的值。但是,我相信你为动画设定的按钮已经有了不透明度1.0。因此,即使动画正在运行,也不会看到任何更改。因此,请尝试淡出转换来测试代码是否有效
其次,您没有设置所有这些东西来设置目标:
SetTarget
,SetTargetName
或animation.TargetName=
你只要用其中一个
第三,不使用新时间跨度(4)
设置持续时间
属性。我不知道您使用的构造函数返回的是什么类型的TimeSpan
,但它不适用于这种情况,它适用于类似TimeSpan.fromMillimes(500)
的内容
第四,你想用fadinmeanimation
的BeginTime
属性做什么?即使您有意使用它,TimeSpan
问题也适用于此
所以,在所有这些之后,试着这样做:
InitializeComponent();
Button button = new Button() {
Width = 200,
Height = 200,
Content = "Text",
Background = new SolidColorBrush(Colors.Red)
};
button.Name = "target";
Storyboard myStory = new Storyboard();
FadeInThemeAnimation animation = new FadeInThemeAnimation() {
BeginTime = new TimeSpan(2),
Duration = new TimeSpan(4),
};
animation.TargetName = button.Name;
Storyboard.SetTarget(animation, button);
Storyboard.SetTargetProperty(animation, button.Name);
myStory.Children.Add(animation);
myGrid.Children.Add(button);
button.PointerEntered += (s, e) => {
myStory.Begin();
};
Storyboard myStory = new Storyboard();
FadeOutThemeAnimation animation = new FadeOutThemeAnimation()
{
Duration = TimeSpan.FromMilliseconds(500)
};
Storyboard.SetTarget(animation, button);
myStory.Children.Add(animation);
PlayGround.PointerPressed += (kk, kkk) => myStory.Begin();
我已经测试过了,它很有效。希望有帮助。问题是?@kennyzx我没有看到任何褪色动画。另外,我不知道在哪里调用story.Begin()以及绑定是否正确。非常感谢!我还检查了TimeSpan
构造函数,单位是tick,即100纳秒。所以我认为时间跨度很短,无论如何都看不见。再次感谢你的帮助!