[英]Windows Phone - popup animation on tap
I'm working on a popup with slide-in and slide-out effects. 我正在研究一种带有滑入和滑出效果的弹出窗口。
I figured out the "in" part. 我想出了“中”部分。 I added an EventTrigger to begin a storyboard when the control's grid loads up. 我添加了一个EventTrigger以在控件的网格加载时开始一个故事板。
<Grid.Triggers>
<EventTrigger RoutedEvent="Grid.Loaded">
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Storyboard.TargetName="SlideTransform"
Storyboard.TargetProperty="Y"
From="1000" To="0" Duration="0:0:0.25"/>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Grid.Triggers>
<Grid.RenderTransform>
<TranslateTransform x:Name="SlideTransform" Y="1000"/>
</Grid.RenderTransform>
I also bind a ViewModel command to the tap event to close the popup: 我还将ViewModel命令绑定到tap事件以关闭弹出窗口:
<i:Interaction.Triggers>
<i:EventTrigger EventName="Tap">
<cmd:EventToCommand Command="{Binding DismissCommand}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
This command is also used on a button placed below this grid (in the "LayoutRoot" master grid). 此命令还用于放置在该网格下方的按钮上(在“ LayoutRoot”主网格中)。
All of that works fine. 所有这些都很好。
Now here's the problem - I need to add a "slide-out" animation BEFORE the popup gets closed. 现在是问题所在-在弹出窗口关闭之前,我需要添加“滑出”动画。 I can't add 我无法添加
<EventTrigger RoutedEvent="Grid.Tap">
Because I get a xaml parse exception at startup: 因为我在启动时收到xaml解析异常:
Additional information: Failed to assign to property 'System.Windows.EventTrigger.RoutedEvent'. 附加信息:无法分配给属性“ System.Windows.EventTrigger.RoutedEvent”。
I tried adding a "Tap" event handler to the whole control, but it seems like the ViewModel command gets triggered first and closes the whole popup. 我尝试向整个控件中添加“ Tap”事件处理程序,但似乎首先触发了ViewModel命令并关闭了整个弹出窗口。
Any ideas on how I could put it all together? 关于如何将所有内容组合在一起的任何想法?
So I figured it out as well, in case someone's interested. 因此,如果有人感兴趣,我也想出了办法。
On each button's Tap event I call a method, that prepares and triggers the animation: 在每个按钮的Tap事件上,我调用一个方法,该方法准备并触发动画:
private void DismissButton_Tap(object sender, System.Windows.Input.GestureEventArgs e)
{
this.dismissAll = false;
this.PrepareAndStartSlideOutAnimation();
}
private void DismissAllButton_Tap(object sender, System.Windows.Input.GestureEventArgs e)
{
this.dismissAll = true;
this.PrepareAndStartSlideOutAnimation();
}
private void PrepareAndStartSlideOutAnimation()
{
Storyboard s = new Storyboard();
DoubleAnimation animation = new DoubleAnimation();
animation.From = 0;
animation.To = 1000;
animation.Duration = new Duration(TimeSpan.FromMilliseconds(250));
LayoutRoot.RenderTransform = new CompositeTransform();
Storyboard.SetTarget(animation, LayoutRoot);
Storyboard.SetTargetProperty(animation, new PropertyPath("(UIElement.RenderTransform).(CompositeTransform.TranslateY)"));
s.Completed += SlideOutAnimation_Completed;
s.Children.Add(animation);
s.Begin();
}
and when the animation completes, I execute a command from the DataContext: 动画完成后,我从DataContext执行命令:
private void SlideOutAnimation_Completed(object sender, EventArgs e)
{
if (!this.dismissAll)
(this.DataContext as HelpOverlayControlViewModel).DismissCommand.Execute(null);
else
(this.DataContext as HelpOverlayControlViewModel).DismissAllCommand.Execute(null);
}
It's not the most "MVVMy" of solutions, but I guess popup animations aren't really the ViewModel's concern, so I'll leave it as it is. 它不是解决方案中最“ MVVMy”的,但是我想弹出动画并不是ViewModel真正关心的问题,因此我将保持现状。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.