Storyboard.Completed和OnNavigatingFrom。如何正确制作动画? [英] Storyboard.Completed and OnNavigatingFrom. How to do animation correctly?
问题描述
当用户在应用程序的某个页面中导航时,我想制作简单的动画。这是我目前拥有的东西:
XAML:
< ; Grid x:Name = LayoutRoot Background = Transparent>
< Grid.Projection>
< PlaneProjection CenterOfRotationY = 1 />
< /Grid.Projection>
< Grid.Resources>
< Storyboard x:Name = OnNavTo>
< DoubleAnimation
Storyboard.TargetName = LayoutRoot
Storyboard.TargetProperty =(Projection)。(PlaneProjection.RotationX)
From =-110 To = 0持续时间= 0:0:0.3 />
< / Storyboard>
< Storyboard x:Name = OnNavFrom>
< DoubleAnimation
Storyboard.TargetName = LayoutRoot
Storyboard.TargetProperty =(Projection)。(PlaneProjection.RotationX)
From = 0 To =- 110 Duration = 0:0:1.3 />
< / Storyboard>
< /Grid.Resources>
< phone:Pivot标题= {Binding LocalizedResources.ApplicationTitle,Source = {StaticResource LocalizedStrings}}>
<!-我的页面内容->
< / phone:Pivot>
< / Grid>
C#:
受保护的覆盖无效OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
OnNavTo.Begin();
}
受保护的覆盖无效OnNavigatedFrom(NavigationEventArgs e)
{
base.OnNavigatedFrom(e);
OnNavFrom.Begin();
}
OnNavigatedTo可以正常工作。当我点击按钮以打开适当的页面时,它将以动画形式打开。但是,当我按下后退按钮时,页面只是关闭了,没有任何动画。
请指出正确的方向
(我是后端python开发人员,绝对不是Windows Phone和.NET中的菜鸟)。 p>
UPD
gaurav5430指出。
首先:我注意到,我使用的是 OnNavigatedFrom
而不是 OnNavigatingFrom
第二个:动画是异步的,因此我需要等到它完成后才能真正关闭页面。至此,我得出以下结论:
XAML:< Storyboard x:Name = OnNavFrom Completed = OnNavFrom_Completed>
C#:
私人布尔cancelExit = true;
受保护的覆盖无效OnNavigatingFrom(NavigatingCancelEventArgs e)
{
e.Cancel = cancelExit;
if(cancelExit)
{
OnNavFrom.Begin();
}
其他
{
base.OnNavigatingFrom(e);
}
}
private void OnNavFrom_Completed(object sender,EventArgs e)
{
cancelExit = false;
NavigationService.GoBack();
}
正在运行。虽然我知道这不是处理事件的最佳方法。我很确定C#可以通过很好的方法来执行此操作,而不必两次调用 NavigationService
(在用户按下回车键时第一次调用,在动画完成时第二次调用)。
gaurav5430提供的链接包含代码示例。但是我不明白它是如何工作的。我认为对于像我这样的新手来说,这段代码太小了,无法理解背后的原因。
所以
很有可能页面已从中导航并且不等待动画完成。
我建议在调用 base.OnNavigatedFrom(e)
之前先调用所有函数,因为这可能是设置导航的函数,因此基本上可以尝试
受保护的重写void OnNavigatedFrom(NavigationEventArgs e)
{
OnNavFrom.Begin();
base.OnNavigatedFrom(e);
}
请注意,这仍然不能确保它等待让动画完成,因为动画可能会异步播放。因此,也许您可以尝试在调用 base.OnNavigatedFrom()
之前添加一些延迟,也可以检查此链接
http://www.silverlightshow.net/items/Windows-Phone-7-Part-3-Understanding-navigation.aspx
I want to make simple animation when user is navigated to and from some page in application. Here is what I have for now:
XAML:
<Grid x:Name="LayoutRoot" Background="Transparent">
<Grid.Projection>
<PlaneProjection CenterOfRotationY="1"/>
</Grid.Projection>
<Grid.Resources>
<Storyboard x:Name="OnNavTo">
<DoubleAnimation
Storyboard.TargetName="LayoutRoot"
Storyboard.TargetProperty="(Projection).(PlaneProjection.RotationX)"
From="-110" To="0" Duration="0:0:0.3" />
</Storyboard>
<Storyboard x:Name="OnNavFrom">
<DoubleAnimation
Storyboard.TargetName="LayoutRoot"
Storyboard.TargetProperty="(Projection).(PlaneProjection.RotationX)"
From="0" To="-110" Duration="0:0:1.3" />
</Storyboard>
</Grid.Resources>
<phone:Pivot Title="{Binding LocalizedResources.ApplicationTitle, Source={StaticResource LocalizedStrings}}">
<!-- My page stuff here -->
</phone:Pivot>
</Grid>
C#:
protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
OnNavTo.Begin();
}
protected override void OnNavigatedFrom(NavigationEventArgs e)
{
base.OnNavigatedFrom(e);
OnNavFrom.Begin();
}
OnNavigatedTo works fine. When I tap button to open appropriate page, it opens with animation. But, when I press back button, page is simply closed, without any animation. Please point me in correct direction
(I'm rather back-end python developer, and absolutely noob in Windows Phone and .NET stuff).
UPD As pointed by gaurav5430.
First: I have noticed, that I'm using OnNavigatedFrom
instead of OnNavigatingFrom
Second: Animation is asynchronous, so I need to wait till it is finished before close the page actually. At this point, I came to the following:
XAML: <Storyboard x:Name="OnNavFrom" Completed="OnNavFrom_Completed">
C#:
private bool cancelExit = true;
protected override void OnNavigatingFrom(NavigatingCancelEventArgs e)
{
e.Cancel = cancelExit;
if (cancelExit)
{
OnNavFrom.Begin();
}
else
{
base.OnNavigatingFrom(e);
}
}
private void OnNavFrom_Completed(object sender, EventArgs e)
{
cancelExit = false;
NavigationService.GoBack();
}
It's working. Though I understand it's not the best way to deal with events. I'm pretty sure C# has nice ways to do this without calling NavigationService
twice (1st when user press back and 2nd when animation is complete).
The link provided by gaurav5430 contains example of code. But I don't understand how it works. I think the piece of code is too small, for novice like me, to understand what is behind the scene.
So can
It is quite possible that the page has been navigated from and does not wait for the animation to complete.
what i would suggest is to call all your functions before calling base.OnNavigatedFrom(e)
, because that is probably the function which sets up the navigation, so basically you can try
protected override void OnNavigatedFrom(NavigationEventArgs e)
{
OnNavFrom.Begin();
base.OnNavigatedFrom(e);
}
please note that this still does not make sure that it waits for your animation to complete, as the animation would probably be played async. so maybe you could try adding some delay before calling base.OnNavigatedFrom()
also you might check this link out
http://www.silverlightshow.net/items/Windows-Phone-7-Part-3-Understanding-navigation.aspx
这篇关于Storyboard.Completed和OnNavigatingFrom。如何正确制作动画?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!