我使用BackgroundWorker
时出错。我使用ListView
控件显示列表,使用ItemsControl
显示所选文件的某些数据。
当SelectionChange
中的ListView
然后ItemsControl
应显示所选项目的数据但会出现错误时: -
错误 - 必须在与DependencyObject相同的线程上创建DependencySource。
这是我的代码:
Variableholder vh=new Variableholder();
public FileView(string path)
{
InitializeComponent();
loadlist(path);
listview.ItemsSource = vh.list();
}
BackgroundWorker bworker = new BackgroundWorker();
public void loadlist(string path)
{
bworker.WorkerSupportsCancellation = true;
bworker.WorkerReportsProgress = true;
bworker.DoWork += bworker_Explorer;
bworker.ProgressChanged += bworker_AddItem;
bworker.RunWorkerCompleted += worker_RunWorkerCompleted;
bworker.RunWorkerAsync(path);
}
public void bworker_Explorer(object sender,DoWorkEventArgs e)
{
ParallelOptions po = new ParallelOptions();
po.MaxDegreeOfParallelism = 10;
Parallel.ForEach(e.Argument.ToString().Split(','),po, speratepath =>
{
......
}); }
public void bworker_AddItem(object sender,ProgressChangedEventArgs e)
{
if (e.UserState != null)
{
vh.list().Add(e.UserState as MusicTags);
}
}
void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
.......
}
private void listview_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
object o = listview.SelectedItem;
string filepath = (o as Tags).Filepath;
itemscontrol.ItemsSource = vh.list().Where(s => s.Filepath == filepath);
}
我做什么?
答案 0 :(得分:0)
由于这是一篇很老的帖子,我猜你已经找到了解决方案,但对于其他人来说,这个问题磕磕绊绊:
您应该在主线程上创建MusicTags
项。仅将它们添加到主线程上的vh.list()