gpt4 book ai didi

c# - 如何停止动画WPF?

转载 作者:行者123 更新时间:2023-11-30 14:15:49 28 4
gpt4 key购买 nike

如何停止动画以使其不会产生 Completed 事件。这是一个简单的例子

<Window x:Class="WpfApplication5.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="248" Width="318">
<Grid>
<Border Width="20" Height="20" Background="Red" MouseEnter="Border_MouseEnter" MouseLeave="Border_MouseLeave" x:Name="border" />
</Grid>
</Window>

和支持代码:

private void Border_MouseEnter(object sender, MouseEventArgs e)
{
var a = new DoubleAnimation { To = 0, Duration = TimeSpan.FromMilliseconds(4000) };
a.Completed += (obj, args) => MessageBox.Show("Boom!");
border.BeginAnimation(Border.OpacityProperty, a);
}

private void Border_MouseLeave(object sender, MouseEventArgs e)
{
border.BeginAnimation(Border.OpacityProperty, null);
border.Opacity = 1;
}

如果我在矩形变白之前将鼠标移出,它仍会在一段时间后显示弹出窗口。如何防止这种情况?假设 Border_MouseLeaveBorder_MouseEnter 方法彼此不了解(它们不能将动画实例变量传递给彼此)。

最佳答案

你可以使用这个:

<Border Width="20" Height="20" Background="Red" x:Name="border" >
<Border.Triggers>
<EventTrigger RoutedEvent="MouseEnter">
<BeginStoryboard Name="Ali">
<Storyboard>
<DoubleAnimation To="0" Duration="0:0:4" Completed="com" Storyboard.TargetProperty="Opacity"/>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
<EventTrigger RoutedEvent="MouseLeave">
<StopStoryboard BeginStoryboardName="Ali"/>
</EventTrigger>
</Border.Triggers>
</Border>

和:

private void com(object sender, EventArgs e)
{
MessageBox.Show("boom!");
}

关于c# - 如何停止动画WPF?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9176155/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com