如何为ListBoxItem创建Live Tile效果?

时间:2011-08-30 09:46:21

标签: c# wpf animation storyboard listboxitem

我想为The Live Tile Effect创建一个ListBox

我考虑过在0和ListBox.Items.Count之间创建一个随机数,然后取出ListBoxItem并为其设置动画。

我正在使用this进行动画制作。

因此,XAML中的基本动画将如下所示:

xmlns:pl="clr-namespace:Planerator"

<pl:Planerator x:Name="planerator">
                        <Image Name="logo" Source="somePath">
                            <Image.Triggers>
                                <EventTrigger RoutedEvent="Image.MouseLeave">
                                    <BeginStoryboard>
                                        <Storyboard>
                                            <DoubleAnimation Storyboard.TargetName="planerator" 
                                                             Storyboard.TargetProperty="RotationX"
                                                             From="0" To="360"
                                                             BeginTime="0:0:0"
                                                             Duration="0:0:1"  
                                                             AutoReverse="False"/>
                                        </Storyboard>
                                    </BeginStoryboard>
                                </EventTrigger>
                            </Image.Triggers>
                        </Image>
                    </pl:Planerator>

这是我到目前为止所尝试的:

<ListBox Name="someList"
         Loaded="someList_Loaded">
</ListBox>

someList_Loaded方法:

    Random random = new Random();
    Planerator planerator = new UI.WPF.Planerator();
    planerator.Name = "planerator";

    someList.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Background,
            new Action(
              delegate()
              {
          do
          {
              //planerator.Child = (ListBoxItem)someList.Items[random.Next(0, someList.Items.Count - 1)];

              DoubleAnimation myDoubleAnimation = new DoubleAnimation()
               { From = 0, To = 360, Duration = new Duration(TimeSpan.FromMilliseconds(1)) };

              Storyboard.SetTargetName(planerator, planerator.Name);
              Storyboard.SetTargetProperty(myDoubleAnimation, new PropertyPath("RotationX"));

              Storyboard storyboard = new Storyboard();
              storyboard.Children.Add(myDoubleAnimation);

              someList.MouseMove += delegate(object newSender, MouseEventArgs args)
              {
                  storyboard.Begin(currentListItem);
              };

              //Sleep 30sec
          }while(true);
      }
  ));

现在我的问题:

  1. 线程正文中的第一行。该投射无效。我如何获得随机ListBoxItem并将其分配给planerator.Child

  2. 这一行//Sleep 30sec:我如何在这里添加等待或睡眠?

  3. 创建The Live Tile Effect,这种方式是可行的还是有更好的方法?

  4. 是否会有任何重大的性能问题?

  5. 修改

    1. ItemContainerGenerator generator = someList.ItemContainerGenerator; ListBoxItem currentListItem = generator.ContainerFromIndex(random.Next(0, someList.Items.Count - 1)) as ListBoxItem;
    2. 修改

      这是我到目前为止所得到的,但没有任何反应:

      lstNotifications.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Background,
                          new Action(
                              delegate()
                              {
                                  ItemContainerGenerator generator = lstNotifications.ItemContainerGenerator;
                                  ListBoxItem currentListItem = generator.ContainerFromIndex(random.Next(0, lstNotifications.Items.Count - 1)) as ListBoxItem;
      
                                  var tempObject = Content;
                                  Content = null;
      
                                  planerator.Child = currentListItem;
      
                                  Content = tempObject;
      
                                  this.RegisterName(planerator.Name, planerator);
      
                                  DoubleAnimation myDoubleAnimation = new DoubleAnimation() { From = 0, To = 360, Duration = new Duration(TimeSpan.FromMilliseconds(1)) };
      
                                  Storyboard.SetTargetName(planerator, planerator.Name);
                                  Storyboard.SetTargetProperty(myDoubleAnimation, new PropertyPath(Planerator.RotationXProperty));
      
                                  Storyboard storyboard = new Storyboard();
                                  storyboard.RepeatBehavior = RepeatBehavior.Forever;
                                  storyboard.Children.Add(myDoubleAnimation);
      
                                  storyboard.Begin(currentListItem);
                              }
                          ));
      

      知道为什么没有发生什么事吗?

1 个答案:

答案 0 :(得分:1)

我不确定你想要实现的目标,但这里有一些可能有所帮助的东西:

1 - 据我所知,Planerator.Child不是ListBoxItem类型 - 所以指定类型faill - 检查该属性的类型。

2 - 您是否尝试过使用System.Threading.Thread.Sleep(30000); ?

3和4我无法回答,但也许Greg Schechter可以提供帮助:

Dead Simple 3D in WPFImplementation Details Of The Planerator