C# 在silverlight中启动动画
我创建了一个Silverlight页面,它有两个动画:一个在页面打开时显示,另一个在页面关闭前显示。我还有另一个主页,其中有一个导航框架,可以容纳这些页面 单击菜单按钮时,切换工作正常。我有一个页面加载的eventhandler,它显示第一个动画,但是我对“消失动画”的调用有问题 我在主页上做了以下事情:C# 在silverlight中启动动画,c#,silverlight-4.0,C#,Silverlight 4.0,我创建了一个Silverlight页面,它有两个动画:一个在页面打开时显示,另一个在页面关闭前显示。我还有另一个主页,其中有一个导航框架,可以容纳这些页面 单击菜单按钮时,切换工作正常。我有一个页面加载的eventhandler,它显示第一个动画,但是我对“消失动画”的调用有问题 我在主页上做了以下事情: menuButtonClicked = (HyperlinkButton)sender; switch (menuButtonClicked.Name)
menuButtonClicked = (HyperlinkButton)sender;
switch (menuButtonClicked.Name)
{
case "homeButton":
{
About aboutPage = (About)ContentFrame.Content;
aboutPage.DisappearAnimation.Begin();
}
break;
case "aboutButton":
{
Home homePage = (Home)ContentFrame.Content;
homePage.DisappearAnimation.Begin();
}
break;
}
但是我得到一个错误,targetProperty无法解析
下面是如何定义动画的
<Storyboard x:Name="DisappearAnimation">
<ObjectAnimationUsingKeyFrames
Storyboard.TargetProperty="(UIElement.Visibility)"
Storyboard.TargetName="PageScrollViewer">
<DiscreteObjectKeyFrame KeyTime="0">
<DiscreteObjectKeyFrame.Value>
<Visibility>Visible</Visibility>
</DiscreteObjectKeyFrame.Value>
</DiscreteObjectKeyFrame>
</ObjectAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames
Storyboard.TargetProperty="(UIElement.RenderTransform)
.(CompositeTransform.TranslateY)"
Storyboard.TargetName="PageScrollViewer">
<EasingDoubleKeyFrame KeyTime="0"
Value="0"/>
<EasingDoubleKeyFrame KeyTime="0:0:0.5"
Value="-14"/>
<EasingDoubleKeyFrame KeyTime="0:0:1"
Value="442"/>
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames
Storyboard.TargetProperty="(UIElement.Opacity)"
Storyboard.TargetName="PageScrollViewer">
<EasingDoubleKeyFrame KeyTime="0:0:0.5"
Value="1"/>
<EasingDoubleKeyFrame KeyTime="0:0:1"
Value="0"/>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
但它也不起作用。。。有人知道我如何调用在另一个页面上定义的开始动画吗
编辑:
下面是我为解决这个问题所做的工作,让事情变得更清楚一点。
Home、About、Comet是我向用户显示的xaml页面,ContentFrame是承载这些页面的navigationFrame
flipanimation只是将导航框翻转360度,使其看起来更漂亮:)
现在我只想弄明白为什么消失动画在不到半秒的时间内完成。。。但是动画开始的顺序(当前站点消失,下一个开始它的显示动画)是可以的
希望这对其他人有所帮助两个页面中都有名为PageScrollViewer的控件吗?我有点保留了这个问题…为什么不为按钮/菜单按钮连接一个RelayCommand或单击事件,而不是使用开关/案例?我首先让它们都使用一个事件处理程序。。。现在我用不同的,所以我摆脱了开关盒。。。我现在使用switch case的唯一原因是确定当前活动的页面,以便我可以开始它们的消失动画。。。我现在把它改成了最后一个版本
aboutPage.SetValue(Storyboard.TargetNameProperty, "PageScrollViewer");
private void HomeButton_MouseLeftButtonDown(object sender, RoutedEventArgs e)
{
if (sender != null && menuButtonClicked != (HyperlinkButton)sender)
{
ChangeMenuState(sender);
ChangePage(currentPage);
currentPage = CurrentPage.Home;
}
}
private void AboutButton_MouseLeftButtonDown
(object sender, RoutedEventArgs e)
{
if (sender != null && menuButtonClicked != (HyperlinkButton)sender)
{
ChangeMenuState(sender);
ChangePage(currentPage);
currentPage = CurrentPage.About;
}
}
private void ContactButton_MouseLeftButtonDown
(object sender, RoutedEventArgs e)
{
if (sender != null && menuButtonClicked != (HyperlinkButton)sender)
{
ChangeMenuState(sender);
ChangePage(currentPage);
currentPage = CurrentPage.Contact;
}
}
private void ChangePage(CurrentPage currentPage)
{
switch (currentPage)
{
case CurrentPage.Welcome:
{
ContentHolder.Visibility = Visibility.Visible;
FlipAnimation.Stop();
FlipAnimation.SetValue(Storyboard.TargetNameProperty,
"ContentHolder");
FlipAnimation.Begin();
}
break;
case CurrentPage.Home:
{
ContentHolder.Visibility = Visibility.Visible;
Home homePage = (Home)ContentFrame.Content;
homePage.DisappearAnimation.Begin();
homePage.DisappearAnimation.Completed += StopStoryboard;
}
break;
case CurrentPage.About:
{
ContentHolder.Visibility = Visibility.Visible;
About aboutPage = (About)ContentFrame.Content;
aboutPage.DisappearAnimation.Begin();
aboutPage.DisappearAnimation.Completed += StopStoryboard;
}
break;
case CurrentPage.Contact:
{
ContentHolder.Visibility = Visibility.Visible;
Contact contactPage = (Contact)ContentFrame.Content;
contactPage.DisappearAnimation.Begin();
contactPage.DisappearAnimation.Completed += StopStoryboard;
}
break;
}
}
private void ChangeMenuState(object sender)
{
VisualStateManager.GoToState(sender as HyperlinkButton,
"ActiveLink", true);
(sender as HyperlinkButton).Foreground =
new SolidColorBrush(Colors.White);
MouseEnterAnimation.Stop();
if (menuButtonClicked != null)
{
VisualStateManager.GoToState(menuButtonClicked,
"InactiveLink", true);
MouseLeaveAnimation.Stop();
MouseLeaveAnimation.SetValue(Storyboard.TargetNameProperty,
menuButtonClicked.Name);
MouseLeaveAnimation.Begin();
}
menuButtonClicked = (HyperlinkButton)sender;
}
private void StopStoryboard(object sender, EventArgs args)
{
Storyboard storyboard = (Storyboard)sender;
storyboard.Stop();
FlipAnimation.Stop();
FlipAnimation.SetValue(Storyboard.TargetNameProperty,
"ContentHolder");
FlipAnimation.Begin();
}