我想为我的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);
      }
  ));


现在我的问题是:


线程主体中的第一行。该转换无效。我将如何获取随机的ListBoxItem并将其分配给planerator.Child
//Sleep 30sec这行:我如何在这里添加等待或睡眠?
对于创建The Live Tile Effect,此方法可行还是有更好的方法?
是否会有重大的性能问题?


编辑:


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


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

最佳答案

不确定您要实现的目标,但是以下一些事情可能会或可能不会有帮助:

1-据我所知,Planerator.Child的类型不是ListBoxItem-因此将其分配为faills类型-请检查该属性的类型。

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

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

Dead Simple 3D in WPFImplementation Details Of The Planerator

关于c# - 如何创建ListBoxItem的动态图块效果?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/7241361/

10-10 10:43