Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何以编程方式在UWP中使用FadeInAnimation.class?_C#_.net_Uwp_Fadein - Fatal编程技术网

C# 如何以编程方式在UWP中使用FadeInAnimation.class?

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

我正在尝试以编程方式进行一个简单的淡入动画。 在XAML中,我有一个包含命名网格的页面

后面的代码如下所示:

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纳秒。所以我认为时间跨度很短,无论如何都看不见。再次感谢你的帮助!