【发布时间】:2015-10-19 14:03:43
【问题描述】:
BackgroundWorker 完成后,我需要更新我的 Canvas。当我通过按钮动作监听器更新画布时,它工作得很好。但是当我在 backroundWorker 完成时调用我的更新函数时。 gui不会更新。
向画布添加项目的函数:
public void AddItemsToCanvas()
{
int factor = 0;
cvList.Children.Clear();
foreach (ItemControl item in ItemControl.ItemsList)
{
cvList.Children.Add(item.updateName);
Canvas.SetLeft(item.updateName, 13);
Canvas.SetTop(item.updateName, factor + 10);
Canvas.SetZIndex(item.updateName, 1);
cvList.Children.Add(item.imageButton);
Canvas.SetLeft(item.imageButton, 180);
Canvas.SetTop(item.imageButton, factor + 13);
Canvas.SetZIndex(item.imageButton, 5);}}
调用更新函数的按钮:
private void Button_Click_1(object sender, RoutedEventArgs e)
{
updateCanvas();
}
public void updateCanvas()
{
Service serv = new Service();
serv.SuperlinkCompartments();
//Check for updates and add them to a List of updates
Update up = new Update();
up.PropertyChanged += new PropertyChangedEventHandler(Items_PropertyChanged);
up.ShowUpdates();
}
public void Items_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
AddItemsToCanvas();
}
当从按钮 actionListener 调用时,Canvas 会完美更新。但是当从 BackgroundWorker 事件调用 UpdateCanvas() 时不起作用。
public void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
var item = dic3[(BackgroundWorker)sender];
int index = (int)item.controlButton.Tag;
item.Progressbar.Value = 0;
SystemInfo.SystemList[Update.UpdateList[index].SystemIndex].Version = Update.UpdateList[index].UpdateVersion;
lvSystems.ItemsSource = SystemInfo.SystemList;
updateCanvas();
}
我在 BackgroundWorker 事件函数中遗漏了什么?
【问题讨论】:
-
我尝试在我的代码中包含 Dispatcher.BeginInvoke。但不能让它工作。
标签: wpf multithreading user-interface canvas