2010-09-30 260 views
0

我想在窗体上显示一些数据。表单应该淡入5秒,并在完全不透明状态下显示数据10秒,然后在3秒内开始淡出。我已经在c#中编程执行此操作。wpf动画淡入淡出和淡出

请给出建议或示例代码。

感谢 拉朱

回答

1

你会使用一个DoubleAnimationUsingKeyFrames(参见MSDN文档here C#中使用示例)和动画控件的Opacity财产。

+0

我使用DoubleAnimationUsingKeyFrames尝试了动画。虽然淡入进行中,我可以看到某种闪烁。 TranslationAnimation.KeyFrames.Add(new LinearDoubleKeyFrame(0.0,KeyTime.FromTimeSpan(TimeSpan.FromSeconds(0)))); TranslationAnimation.KeyFrames.Add(new LinearDoubleKeyFrame(1.0,KeyTime.FromTimeSpan(TimeSpan.FromSeconds(5))));淡入不顺畅。 – user209293 2010-10-06 05:45:39

+0

我不得不看完整的代码,看看有什么不对。 – bitbonk 2010-10-06 06:20:38

+0

PageViewObj.RegisterName(“Animation”,PageViewObj); sb = new System.Windows.Media.Animation.Storyboard(); sb.BeginTime = TimeSpan.FromMilliseconds(0); TranslationAnimation = new ystem.Windows.Media.Animation.DoubleAnimationUsingKeyFrames(); TranslationAnimation.KeyFrames.Add(new LinearDoubleKeyFrame(0.0,KeyTime.FromTimeSpan(TimeSpan.FromSeconds(0)))); TranslationAnimation.KeyFrames.Add(new LinearDoubleKeyFrame(1.0 ,KeyTime.FromTimeSpan(TimeSpan.FromSeconds(5)))); TranslationAnimation.KeyFrames.Add(new LinearDoubleKeyFrame(1.0,KeyTime.FromTimeSpan(TimeSpan.FromSeconds(10)))); – user209293 2010-10-06 07:06:19

0

您可以在XAML中定义一个操纵不透明度的故事板。下面的完整的XAML的例子说明这一点:

<Page 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    WindowTitle="Fading Rectangle Example"> 
    <StackPanel Margin="10"> 

    <Rectangle 
     Name="MyRectangle" 
     Width="100" 
     Height="100" 
     Fill="Blue"> 
    </Rectangle> 

    <Button Name="BeginButton">Begin</Button> 

    <StackPanel.Triggers> 
     <EventTrigger RoutedEvent="Button.Click" SourceName="BeginButton"> 
     <BeginStoryboard Name="MyBeginStoryboard"> 
      <Storyboard> 
      <DoubleAnimation 
       Storyboard.TargetName="MyRectangle" 
       Storyboard.TargetProperty="(Rectangle.Opacity)" 
       From="1.0" To="0.0" Duration="0:0:5" /> 
      </Storyboard> 
     </BeginStoryboard> 
     </EventTrigger> 
    </StackPanel.Triggers> 
    </StackPanel> 
</Page> 

运行从C#动画,根据您的要求,也有可能:

public void DoAnimation() 
{ 
    Storyboard opacityStoryboard = FindResource("MyBeginStoryboard") as Storyboard; 
    opacityStoryboard.Begin(this); 
} 

两种方法的结合是定义在XAML的动画和在C#中激活它。

使用这个模式,你可以定义两个故事板:

  • 改变窗体的Opacity属性从0.0到1.0以上5秒
  • 窗体的Opacity属性的变化,从1.0到0.0故事板分镜脚本3秒以上

您可以修改上面的例子来做到这一点作为一个独立的样本:

<Page 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    WindowTitle="Fading Rectangle Example"> 
    <StackPanel Margin="10"> 

    <Rectangle 
     Name="MyRectangle" 
     Width="100" 
     Height="100" 
     Fill="Blue"> 
    </Rectangle> 

    <Button Name="FadeInButton">Fade In</Button> 
    <Button Name="FadeOutButton">Fade Out</Button> 

    <StackPanel.Triggers> 
     <EventTrigger RoutedEvent="Button.Click" SourceName="FadeInButton"> 
     <BeginStoryboard Name="FadeInStoryboard"> 
      <Storyboard> 
      <DoubleAnimation 
       Storyboard.TargetName="MyRectangle" 
       Storyboard.TargetProperty="(Rectangle.Opacity)" 
       From="0.0" To="1.0" Duration="0:0:5" /> 
      </Storyboard> 
     </BeginStoryboard> 
     </EventTrigger> 
     <EventTrigger RoutedEvent="Button.Click" SourceName="FadeOutButton"> 
     <BeginStoryboard Name="FadeOutStoryboard"> 
      <Storyboard> 
      <DoubleAnimation 
       Storyboard.TargetName="MyRectangle" 
       Storyboard.TargetProperty="(Rectangle.Opacity)" 
       From="1.0" To="0.0" Duration="0:0:3" /> 
      </Storyboard> 
     </BeginStoryboard> 
     </EventTrigger> 
    </StackPanel.Triggers> 
    </StackPanel> 
</Page> 

使用上面显示的“从C#运行故事板”模式,您可以在适当的时候在C#代码中运行每个故事板。