Home » Featured, Sample Code + Tutorials

Very Slick Windows Phone 7 Staggered Animations [Ex W/Source]

5 November 2010 1,220 views One Comment
Demo of how it looks

After a bit of messing around, I finally discovered a slick way to produce a staggered loading effect for my list boxes. I found the technique from my friend Andrej Tozon’s blog.

Basically, the technique is to use the Reactive framework to asynchronously call a change of the VisualStateManager BeforeItemLoaded and AfterItemLoaded states. The entire code can be summed up with this brief method below. Of course, the XAML for the states and style of the listbox is additional code which can be found here: Download Project Files.

private void ListBox_Loaded(object sender, RoutedEventArgs e)
  {
      ((ListBox)sender).Opacity = 0;

      for (int i = 0; i < ((ListBox)sender).Items.Count; i++)
      {
          ListBoxItem item = (ListBoxItem)((ListBox)sender).ItemContainerGenerator.ContainerFromIndex(i);
          if (item != null)
          {
              VisualStateManager.GoToState(item, "BeforeLoaded", false);
          }
      }

      ((ListBox)sender).Opacity = 1.0;

      var observable = from t in Observable.Interval(TimeSpan.FromMilliseconds(300))
                               .TimeInterval().Take(((ListBox)sender).Items.Count)
                       select t.Value;

      observable.Subscribe(i => Dispatcher.BeginInvoke(() =>
          VisualStateManager.GoToState(
              (ListBoxItem)((ListBox)sender).ItemContainerGenerator
                  .ContainerFromIndex((int)i), "AfterLoaded", true)));
  }

Did you like this? Share it:
1 Star2 Stars3 Stars4 Stars5 Stars (1 votes, average: 5.00 out of 5)
Loading ... Loading ...

One Comment »

Leave your response!

Add your comment below, or trackback from your own site. You can also subscribe to these comments via RSS.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

This is a Gravatar-enabled weblog. To get your own globally-recognized-avatar, please register at Gravatar.