我有一个带倒数字符串的List,我想每隔一秒用一个DispatcherTimer更新它。List <>在DispatcherTimer中没有更新
初始化(的窗口上的负荷运行)
tasks.Add(new Tasks()
{
title = "Task 1",
date = "14:30 17 Martie 2016",
countdown = "1",
timer = new TimeSpan(0, 15, 32)
});
tasks.Add(new Tasks()
{
title = "Task 2",
date = "14:30 17 Martie 2016",
countdown = "2",
timer = new TimeSpan(1, 10, 52)
});
listViewTasks.ItemsSource = tasks;
initCountdown();
类
public class Tasks
{
public string title { get; set; }
public string date { get; set; }
public string countdown { get; set; }
public TimeSpan timer { get; set; }
}
DispatcherTimer
public void initCountdown()
{
string item = tasks[0].title;
_time = tasks[0].timer;
_timer = new DispatcherTimer(new TimeSpan(0, 0, 1), DispatcherPriority.Normal, delegate
{
tasks[0].countdown = _time.ToString("c"); //this does not update
if (_time == TimeSpan.Zero) _timer.Stop();
_time = _time.Add(TimeSpan.FromSeconds(-1));
}, Application.Current.Dispatcher);
_timer.Start();
}
XAML
<ListView Margin="0,30,0,0" Name="listViewTasks">
<ListView.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="1*"/>
</Grid.ColumnDefinitions>
<StackPanel HorizontalAlignment="Left" Margin="0,0,50,0">
<TextBlock Text="{Binding title}" Cursor="Hand"/>
<TextBlock Text="{Binding date}" />
</StackPanel>
<StackPanel HorizontalAlignment="Right" Grid.Column="1">
<TextBlock Text="{Binding countdown}" />
</StackPanel>
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
从调度员外我可以更新它,但从里面不工作。
也不例外,用户界面不会更新
我想更新列表视图,以显示我从我加入的时间跨度倒计时。
你有例外吗?还是只是没有更新? – Zack
这是什么意思“不起作用”?它从未达到零,或者ui不更新? – Liero
@Zack也不例外 – user2252671