views:

110

answers:

2

Hi

I want to show some data on a form. The form should fade in for 5 sec and display data with full opacity for 10 sec and then start fade out in 3 sec. I have do this programatically in c#.

Please give suggestions or sample code.

Thanks Raju

+1  A: 

You would use a DoubleAnimationUsingKeyFrames (see c# usage example in the msdn docs here) and animate the Opacity property of your control.

bitbonk
I tried animation using DoubleAnimationUsingKeyFrames . While fade-in is in progress, i could see some sort of flicker. TranslationAnimation.KeyFrames.Add(new LinearDoubleKeyFrame(0.0, KeyTime.FromTimeSpan(TimeSpan.FromSeconds(0)))); TranslationAnimation.KeyFrames.Add(new LinearDoubleKeyFrame(1.0, KeyTime.FromTimeSpan(TimeSpan.FromSeconds(5)))); The fade in is not smooth.
I'd have to see the full code to see what's wrong.
bitbonk
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))));
//set tartget name System.Windows.Media.Animation.Storyboard.SetTargetProperty(TranslationAnimation, new System.Windows.PropertyPath System.Windows.Controls.UserControl.OpacityProperty)); sb.Children.Add(TranslationAnimation);Based on Mouse event i will call sb.Begin(PageViewObj, true);
You said you wanted it to fade out for 3 seconds, so you forgot one keyframe: `anim.KeyFrames.Add(new LinearDoubleKeyFrame(0.0, KeyTime.FromTimeSpan(TimeSpan.FromSeconds(0))));` `anim.KeyFrames.Add(new LinearDoubleKeyFrame(1.0, KeyTime.FromTimeSpan(TimeSpan.FromSeconds(5))));` `anim.KeyFrames.Add(new LinearDoubleKeyFrame(1.0, KeyTime.FromTimeSpan(TimeSpan.FromSeconds(15))));` `anim.KeyFrames.Add(new LinearDoubleKeyFrame(0.0, KeyTime.FromTimeSpan(TimeSpan.FromSeconds(18))));` Also you should set the `Duration` of your anim to 18s
bitbonk
A: 

You can define a storyboard in XAML that manipulates the Opacity. The following complete XAML example illustrates this:

  <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>

Running the animation from C#, per your requirements, is also possible:

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

A combination of the two approaches is to define the animation in XAML and activate it in C#.

Using this pattern, you can define two storyboards:

  • A storyboard that changes the form's Opacity property from 0.0 to 1.0 over five seconds
  • A storyboard that changes the form's Opacity property from 1.0 to 0.0 over three seconds

You can modify the example above to do this as a standalone sample:

<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>

Using the "run storyboard from C#" pattern shown above, you can run each of the storyboards at the proper time in your C# code.

JeffFerguson