如何为ListBoxItem创建动态平铺效果?

本文关键字:动态 ListBoxItem 创建 | 更新日期: 2023-09-27 18:06:55

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

我想在0和ListBox.Items.Count之间创建一个随机数,然后取ListBoxItem并将其动画化。

我正在使用这个动画。

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. 会有什么主要的性能问题吗?

编辑:

  1. ItemContainerGenerator generator = someList.ItemContainerGenerator; ListBoxItem currentListItem = generator.ContainerFromIndex(random.Next(0, someList.Items.Count - 1)) as ListBoxItem;
编辑:

这是我到目前为止得到的,但是什么也没有发生:

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);
                        }
                    ));

知道为什么什么都没发生吗?

如何为ListBoxItem创建动态平铺效果?

我不知道你想达到什么目的,但这里有一些事情可能会有帮助,也可能没有帮助:

1 -据我所知Planerator。子属性不是ListBoxItem类型的,所以给它分配ListBoxItem类型会失败,请检查该属性的类型。

2 -你试过使用System.Threading.Thread.Sleep(30000)吗?

?

3和4我不能回答,但也许Greg Schechter可以帮忙:

Dead Simple 3D in WPF and The Planerator的实现细节