[英]WP7 UI updating issue
我正在动态创建5x5网格,并尝试在这些单元格上创建动画。 当我从程序创建开/关效果时。 我可以看到所有单元都同时打开或关闭,而不是单独打开。
这是代码。 我在这里做错了什么?
private CellControl[,] Lights;
private void CreateGrid()
{
for (int i = 0; i < 5; i++)
{
ColumnDefinition cd = new ColumnDefinition() { Width = new GridLength(20, GridUnitType.Star) };
RowDefinition rd = new RowDefinition() { Height = new GridLength(20, GridUnitType.Star) };
this.GridGame.ColumnDefinitions.Add(cd);
this.GridGame.RowDefinitions.Add(rd);
}
for (int i = 0; i < GridGame.RowDefinitions.Count; ++i)
{
for (int k = 0; k < GridGame.ColumnDefinitions.Count; ++k)
{
var btn = new CellControl();
btn.TabIndex = k + (i * GridSize) + 1;
btn.State = ButtonState.On;
btn.Clicked +=new EventHandler<MouseButtonEventArgs>(btn_Clicked);
Lights[k, i] = btn;
GridGame.Children.Add(btn);
Grid.SetRow(btn, i);
Grid.SetColumn(btn, k);
}
}
}
// Animation goes here
private void AnimateGrid()
{
//Clear the grid
Dispatcher.BeginInvoke( (Action)(()=>
{
for (int y = 0; y < GridSize; y++)
for (int x = 0; x < GridSize; x++)
{
this.Lights[x, y].ToggleOff();
}
}));
int[][] lightSequence = new int[][]
{
new int[] { 1, 1 }, new int[] { 1, 2 }, new int[] { 1, 3 }, new int[] { 1, 4 }, new int[] { 1, 5 },
new int[] { 2, 5 }, new int[] { 3, 5 }, new int[] { 4, 5 }, new int[] { 5, 5 },
new int[] { 5, 4 }, new int[] { 5, 3 }, new int[] { 5, 2 }, new int[] { 5, 1 },
new int[] { 4, 1 }, new int[] { 3, 1 }, new int[] { 2, 1 },
new int[] { 2, 2 }, new int[] { 2, 3 }, new int[] { 2, 4 },
new int[] { 3, 4 }, new int[] { 4, 4 },
new int[] { 4, 3 }, new int[] { 4, 2 },
new int[] { 3, 2 }, new int[] { 2, 2 },
new int[] { 2, 3 }, new int[] { 3, 3 }
};
foreach (int[] item in lightSequence )
{
int x = item[0];
int y = item[1];
x -= 1;
y -= 1;
this.Lights[x, y].ToggleOn();
Thread.Sleep(100);
}
}
问题在于使用Thread.Sleep(100)实际上是在阻止UI线程,因此它不会立即更新。 执行完所有Thread.Sleep(100)后,将更新UI并渲染单元格(全部同时显示)。
您必须在其他线程(例如,使用BackgroundWorker)中运行循环,然后在每次要更新UI时调用Dispatcher。
像这样:
var bw = new BackgroundWorker();
bw.DoWork += (s, e) =>
{
foreach (int[] item in lightSequence)
{
int x = item[0];
int y = item[1];
x -= 1;
y -= 1;
Dispatcher.BeginInvoke((Action<int, int>)((a, b) => this.Lights[a, b].ToggleOn()), x, y);
Thread.Sleep(100);
}
};
bw.RunWorkerAsync();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.