我正在尝试用故事板和双重动画淡出一个UIElement,这个元素确实淡出了,但它看起来是分两步完成的,而不是逐渐改变它的持续时间。它看起来像是从1跳到0.5,再到0,只有3次更改,但我希望它能改变不透明度,比这更平滑。
下面是我当前的代码:
public void fadeout(UIElement target, int targetopac = 0, int fadetime = 1000)
{
Storyboard FadeOut = new Storyboard();
DoubleAnimation Fade = new DoubleAnimation();
Fade.From = target.Opacity;
Fade.To = targetopac;
Fade.Duration = TimeSpan.FromMilliseconds(fadetime);
FadeOut.Children.Add(Fade);
Storyboard.SetTarget(Fade, target);
Storyboard.SetTargetProperty(Fade, new PropertyPath("(UIElement.Opacity)"));
FadeOut.Begin();
}编辑:代码似乎在64位系统上工作得很好,但我在jumps上试用的任何32位系统仍然会发生
发布于 2019-03-06 21:07:28
我不确定为什么您的C#代码不能按预期工作,但是如果XAML代码对您有用,这里有一个用XAML实现的示例。我觉得XAML更清晰、更简洁。在这里,我绑定了在鼠标单击元素时触发的动画,例如:
<Grid Background="Red" >
<Grid.Triggers>
<EventTrigger RoutedEvent="MouseDown">
<BeginStoryboard>
<Storyboard x:Key="">
<DoubleAnimation To="0" Duration="00:00:01" Storyboard.TargetProperty="Opacity" AutoReverse="True" />
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Grid.Triggers>
</Grid>但您当然可以将其附加到任何其他事件,或者通过使用其资源键定位它来从后面的代码调用故事板,如下所示:
<Grid Background="Red" x:Name="MyGrid">
<Grid.Resources>
<Storyboard x:Key="OpacityStoryboard">
<DoubleAnimation To="0" Duration="00:00:01"
Storyboard.Target="{x:Reference Name=MyGrid}"
Storyboard.TargetProperty="Opacity"
AutoReverse="True" />
</Storyboard>
</Grid.Resources>
</Grid>像这样调用:
(MyGrid.Resources["OpacityStoryboard"] as Storyboard).Begin();https://stackoverflow.com/questions/55022281
复制相似问题