2010-09-30 259 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#代碼中運行每個故事板。