Skip to main content

Microsoft Silverlight

Answered Question Storyboard target setting wrong?!RSS Feed

(0)

fyok2002
fyok2002

Member

Member

2 points

5 Posts

Storyboard target setting wrong?!

HI, I don't know if it is a but or not, bug it is strange. I have made an storyboard in XAML(SBsource), like change target's opacity from 1 to 0. and in C# codebehind, I could open image files from my computers(image[ i ]). and I also create storyboard for each image's(SBimage[ i ]). and I just make the storyboard I create in C# to equal to the one I create in XAML then change the target for each SBimage(SBimange[ i ] = SBsource; storyboard.SetTarget(SBimage[ i ],Image[ i ])). but when I running them, the only one will do the animation is the first image. the code as below:

Storyboard create in XAML:
<Storyboard x:Name="SBsource">
   <DoubleAnimationUsingKeyFrames Storyboard.TargetName="" Storyboard.TargetProperty="(UIElement.Opacity)" BeginTime="00:00:00">
    <SplineDoubleKeyFrame x:Name="staValue" KeyTime="00:00:00" Value="1"/>
    <SplineDoubleKeyFrame x:Name="endValue" KeyTime="00:00:00.5000000" Value="0"/>
   </DoubleAnimationUsingKeyFrames>
</Storyboard>

C# CodeBehind:
Storyboard[] SBimage;
SBimage = new Storyboard[image.Length]
for (int i = 0; i < of.Files.Count(); i++)
 {
    image[ i ] = new Image();
    source= new System.Windows.Media.Imaging.BitmapImage();
    source.SetSource(of.Files.ElementAt(i).OpenRead()) ;//System.Windows.Controls.FileDialogFileInfo[])(of.Files))[ i ].OpenRead());
    image[ i ].Source = source;
    GridImgField.Children.Add(image[ i ]);
    Storyboard.SetTarget(SBimage[ i ], image[ i ]);
 }

 private void fade1_Click(object sender, RoutedEventArgs e)
 {
      SBimage[0].Begin();
 }
private void fade2_Click(object sender, RoutedEventArgs e)
{
     SBimage[1].Begin();
}

Here I just add to button to make image[0], and image[1] to do the animation, This is just brif code, but I just show you the idea, but the result is, what ever I click which button, the only image will changed the opacity is the image[1].

bryant
bryant

Star

Star

9937 points

1,629 Posts

Silverlight MVP

Re: Storyboard target setting wrong?!

When you call SBimange[ i ] = SBsource; you're not creating a new storyboard, just pointing to the storyboard that already exists. You need to create a new storyboard for each image for this to work.

-- bryant

Blog | Twitter
_________________
Dont forget to click "Mark as Answer" on the post that helped you.

fyok2002
fyok2002

Member

Member

2 points

5 Posts

Re: Re: Storyboard target setting wrong?!

What you mean about create new storyboard? is just call SBimage[ i ]= new storyboard()? I have tried, it doing same thing. actually I have tried another way it is did same thing. I have create 2 new storyboard:

storyboard SB1 = new storyboard();
storyboard SB2 = new storyboard();
SB1 = SBsource;
SB2 = SBsource;
Storyboard.setTarget(SB1, image1);
Storyboard.setTarget(SB2, image2);

and I create 2 button to make them begin. it still doing same thing.

bryant
bryant

Star

Star

9937 points

1,629 Posts

Silverlight MVP
Answered Question

Re: Re: Storyboard target setting wrong?!

fyok2002:
SB1 = SBsource;
SB2 = SBsource;

You're still doing the same thing. SB1 and SB2 now both point to the same storyboard. You have to create a new storyboard and set the properties on that storyboard, not set it to a different storyboard.

-- bryant

Blog | Twitter
_________________
Dont forget to click "Mark as Answer" on the post that helped you.

fyok2002
fyok2002

Member

Member

2 points

5 Posts

Re: Re: Storyboard target setting wrong?!

a ha, now I get it. thank you.

  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities