添加新项目一定时间后如何为每个 ListViewItem 使用 ColorAnimation
How to use ColorAnimation for each ListViewItem after certain time of adding a new item
我正在开发 Windows 10 应用程序。我使用了一个 ListView,它通过文本框获取它的项目。一段时间后,我需要让 ListViewItems 闪烁(或只是改变颜色)。我使用了 DispatcherTimer。当我 运行 应用程序时,情节提要被执行,但只有 header 闪烁而不是 ListView。如有任何帮助,我们将不胜感激。
这是 XAML 部分:
<Page
x:Class="listViewAnimation.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:listViewAnimation"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Page.Resources>
<Storyboard x:Name="Storyboard1">
<ColorAnimation Storyboard.TargetName="listView" Storyboard.TargetProperty="(ListViewItem.Foreground).(SolidColorBrush.Color)" From="Red" To="YellowGreen" Duration="0:0:0.2"
AutoReverse="True" RepeatBehavior="Forever"/>
</Storyboard>
</Page.Resources>
<Grid Background="Black">
<ListView x:Name="listView" HorizontalAlignment="Left" Height="566" Margin="428,128,0,0" VerticalAlignment="Top" Width="1357" Foreground="#FFF90000" Header="blah blah">
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem" >
<Style.Setters>
<Setter Property="FontSize" Value="370"/>
<Setter Property="FontFamily" Value="Digital-7 Mono"/>
<Setter Property="Foreground" Value="Red"/>
<Setter Property="Width" Value="630"/>
</Style.Setters>
</Style>
</ListView.ItemContainerStyle>
<ListView.ItemsPanel>
<ItemsPanelTemplate>
<ItemsStackPanel
Orientation="Horizontal" />
</ItemsPanelTemplate>
</ListView.ItemsPanel>
</ListView>
<TextBox x:Name="textBox" HorizontalAlignment="Left" Margin="369,899,0,0" TextWrapping="Wrap" Text="14" VerticalAlignment="Top" Height="60" Width="194"/>
<Button x:Name="button" Content="Button" HorizontalAlignment="Left" Margin="631,925,0,0" VerticalAlignment="Top" Background="#FFFBFBFE" BorderBrush="#FFFBFBFE" Click="button_Click"/>
</Grid>
后面的代码如下:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Media.Animation;
using Windows.UI.Xaml.Navigation;
// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409
namespace listViewAnimation
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class MainPage : Page
{
DispatcherTimer t = new DispatcherTimer();
public MainPage()
{
this.InitializeComponent();
}
private void button_Click(object sender, RoutedEventArgs e)
{
listView.Items.Add(textBox.Text);
manip();
}
private void manip()
{
t.Interval = new TimeSpan(0, 0, 2);
t.Tick += T_Tick;
t.Start();
}
private void T_Tick(object sender, object e)
{
Storyboard1.Begin();
}
}
}
注意:另外,我是 Windows 10 开发的新手。
您必须将目标 属性 设置为添加 ListViewItem
而不是 ListView
<Storyboard x:Name="Storyboard1">
<ColorAnimation Storyboard.TargetProperty="(ListViewItem.Foreground).(SolidColorBrush.Color)" From="Red" To="YellowGreen" Duration="0:0:0.2"
AutoReverse="True" RepeatBehavior="Forever"/>
</Storyboard>
ListViewItem item;
private void button_Click(object sender, RoutedEventArgs e)
{
listView.Items.Add(textBox.Text);
listView.UpdateLayout();
item = (ListViewItem) listView.ContainerFromIndex(listView.Items.Count-1);
manip();
}
private void T_Tick(object sender, object e)
{
if (item != null)
{
Storyboard1.Stop();//If animation is running for previously added item
Storyboard.SetTarget(Storyboard1, item);
Storyboard1.Begin();
t.Stop();
}
}
我正在开发 Windows 10 应用程序。我使用了一个 ListView,它通过文本框获取它的项目。一段时间后,我需要让 ListViewItems 闪烁(或只是改变颜色)。我使用了 DispatcherTimer。当我 运行 应用程序时,情节提要被执行,但只有 header 闪烁而不是 ListView。如有任何帮助,我们将不胜感激。
这是 XAML 部分:
<Page
x:Class="listViewAnimation.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:listViewAnimation"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Page.Resources>
<Storyboard x:Name="Storyboard1">
<ColorAnimation Storyboard.TargetName="listView" Storyboard.TargetProperty="(ListViewItem.Foreground).(SolidColorBrush.Color)" From="Red" To="YellowGreen" Duration="0:0:0.2"
AutoReverse="True" RepeatBehavior="Forever"/>
</Storyboard>
</Page.Resources>
<Grid Background="Black">
<ListView x:Name="listView" HorizontalAlignment="Left" Height="566" Margin="428,128,0,0" VerticalAlignment="Top" Width="1357" Foreground="#FFF90000" Header="blah blah">
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem" >
<Style.Setters>
<Setter Property="FontSize" Value="370"/>
<Setter Property="FontFamily" Value="Digital-7 Mono"/>
<Setter Property="Foreground" Value="Red"/>
<Setter Property="Width" Value="630"/>
</Style.Setters>
</Style>
</ListView.ItemContainerStyle>
<ListView.ItemsPanel>
<ItemsPanelTemplate>
<ItemsStackPanel
Orientation="Horizontal" />
</ItemsPanelTemplate>
</ListView.ItemsPanel>
</ListView>
<TextBox x:Name="textBox" HorizontalAlignment="Left" Margin="369,899,0,0" TextWrapping="Wrap" Text="14" VerticalAlignment="Top" Height="60" Width="194"/>
<Button x:Name="button" Content="Button" HorizontalAlignment="Left" Margin="631,925,0,0" VerticalAlignment="Top" Background="#FFFBFBFE" BorderBrush="#FFFBFBFE" Click="button_Click"/>
</Grid>
后面的代码如下:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Media.Animation;
using Windows.UI.Xaml.Navigation;
// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409
namespace listViewAnimation
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class MainPage : Page
{
DispatcherTimer t = new DispatcherTimer();
public MainPage()
{
this.InitializeComponent();
}
private void button_Click(object sender, RoutedEventArgs e)
{
listView.Items.Add(textBox.Text);
manip();
}
private void manip()
{
t.Interval = new TimeSpan(0, 0, 2);
t.Tick += T_Tick;
t.Start();
}
private void T_Tick(object sender, object e)
{
Storyboard1.Begin();
}
}
}
注意:另外,我是 Windows 10 开发的新手。
您必须将目标 属性 设置为添加 ListViewItem
而不是 ListView
<Storyboard x:Name="Storyboard1">
<ColorAnimation Storyboard.TargetProperty="(ListViewItem.Foreground).(SolidColorBrush.Color)" From="Red" To="YellowGreen" Duration="0:0:0.2"
AutoReverse="True" RepeatBehavior="Forever"/>
</Storyboard>
ListViewItem item;
private void button_Click(object sender, RoutedEventArgs e)
{
listView.Items.Add(textBox.Text);
listView.UpdateLayout();
item = (ListViewItem) listView.ContainerFromIndex(listView.Items.Count-1);
manip();
}
private void T_Tick(object sender, object e)
{
if (item != null)
{
Storyboard1.Stop();//If animation is running for previously added item
Storyboard.SetTarget(Storyboard1, item);
Storyboard1.Begin();
t.Stop();
}
}