[英]How to Animate a Control Using the Unloaded Event in WPF
I hate to make another question so soon, but I can't seem to get this to work quite right. 我讨厌这么快就提出另一个问题,但是我似乎无法使这个工作正常进行。
I have a UserControl
with a StoryBoard
set to run when the Unloaded RoutedEvent
occurs. 我有一个带有
StoryBoard
的UserControl
,该UserControl
设置为在发生Unloaded RoutedEvent
时运行。 However, I also have the code so that it will remove the the UserControl
from its containing StackPanel
when a button is clicked. 但是,我也有代码,以便在单击按钮时将
UserControl
从其包含的StackPanel
删除。 What I would like to see happen is when that delete button is clicked, the StoryBoard
runs, then the UserControl
is removed from the StackPanel
. 我想看到的是,单击删除按钮,运行
StoryBoard
, 然后从StackPanel
删除UserControl
。
There's a lot of code already, so I'm not sure what to place. 已经有很多代码,所以我不确定要放置什么。 Let me know what code you want me to place for you to help me.
让我知道您要我放置什么代码以帮助您。
All I have is a RoutedEvent
in the UserControl
that raises the delete (or unload) event: 我所拥有的只是
UserControl
中的RoutedEvent
,它引发了delete(或unload)事件:
public static readonly RoutedEvent deleteEvent =
EventManager.RegisterRoutedEvent("delete", RoutingStrategy.Bubble,
typeof(RoutedEventHandler), typeof(TimeEntry));
private void raiseDeleteEvent()
{
RoutedEventArgs newDeleteEvent = new RoutedEventArgs(deleteEvent,this);
RaiseEvent(newDeleteEvent);
}
private void deleteButton_Click(object sender, RoutedEventArgs e)
{
raiseDeleteEvent();
}
then, two methods in the main window that registers to the event being thrown: 然后,在主窗口中注册发生事件的两个方法:
private void entryAdder_Click(object sender, RoutedEventArgs e)
{
currentSession.addEntry(DateTime.Now);
scrollStack.Children.Add(currentSession.currentTimeEntry);
currentSession.currentTimeEntry.delete += currentTimeEntry_delete;
}
void currentTimeEntry_delete(object sender, RoutedEventArgs e)
{
scrollStack.Children.Remove(sender as TimeEntry);
}
and finally, the UserControl
, with its StoryBoard
s: 最后是
UserControl
及其StoryBoard
:
<Storyboard x:Key="Ondelete">
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleX)" Storyboard.TargetName="UserControl">
<EasingDoubleKeyFrame KeyTime="0" Value="1"/>
<EasingDoubleKeyFrame KeyTime="0:0:0.25" Value="0"/>
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleY)" Storyboard.TargetName="UserControl">
<EasingDoubleKeyFrame KeyTime="0" Value="1"/>
<EasingDoubleKeyFrame KeyTime="0:0:0.25" Value="0"/>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
I figured out how to solve my problem. 我想出了解决问题的方法。 I decided not to use the
Unloaded
event to make the animation, but instead procedurally code the animation, then have the UserControl
subscribe to the Completed
event of that DoubleAnimation
, and finally trigger its own delete event, which would make anything subscribed to that event occur (ie the removal from the visual StackPanel
and list of UserControl
objects). 我决定不使用
Unloaded
事件制作动画,而是对动画进行程序编码,然后让UserControl
订阅该DoubleAnimation
的Completed
事件,最后触发其自己的delete事件,这将使对该事件进行任何订阅的事件都发生(即从可视StackPanel
和UserControl
对象列表中删除)。 This would allow the animation to run, and then, when it is finished, raise the deletion event. 这将允许动画运行,然后在完成时引发删除事件。
Here's the mess of code for that. 这是代码的混乱。 It's all pretty much summed up in these method calls.
这些方法调用中几乎都已总结了这些内容。 They're located within the
UserControl
: 它们位于
UserControl
:
public static readonly RoutedEvent deleteEvent = EventManager.RegisterRoutedEvent("delete", RoutingStrategy.Bubble,
typeof(RoutedEventHandler), typeof(TimeEntry));
private TransformGroup animatedTransform;
private ScaleTransform animatedScale;
private DoubleAnimation deleteDoubleAnimation;
public UserControl()//the constructor
{
timeIn = DateTime.Now;
timeSpent = timeOut - timeIn;
this.InitializeComponent();
setUpRenderTransform();
setUpAnimationVariables();
deleteDoubleAnimation.Completed += deleteDoubleAnimation_Completed;
}
private void setUpRenderTransform()
{
animatedTransform = new TransformGroup();
animatedScale = new ScaleTransform();
animatedTransform.Children.Add(animatedScale);
this.RenderTransform = animatedTransform;
}
private void setUpAnimationVariables()
{
deleteDoubleAnimation = new DoubleAnimation();
deleteDoubleAnimation.To = 0;
deleteDoubleAnimation.Duration = new Duration(TimeSpan.Parse("0:0:.25"));
}
private void raiseDeleteEvent()
{
RoutedEventArgs newDeleteEvent = new RoutedEventArgs(deleteEvent, this);
RaiseEvent(newDeleteEvent);
}
private void deleteButton_Click(object sender, RoutedEventArgs e)
{
deleteAnimation();
}
void deleteDoubleAnimation_Completed(object sender, EventArgs e)
{
raiseDeleteEvent();
}
private void deleteAnimation()
{
animatedScale.BeginAnimation(ScaleTransform.ScaleXProperty, deleteDoubleAnimation);
animatedScale.BeginAnimation(ScaleTransform.ScaleYProperty, deleteDoubleAnimation);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.