我想要为我的 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);
}
));
有什么想法,为什么什么也没有发生?