从名为 voids 的后台工作者更新组合框/列表框数据源
我有大约 5 个空缺来查询数据库,然后设置一些控件的数据源。由于更新控件时 UI 没有冻结,因此我使用了 BackGroundWorker。有没有办法使用相同的空隙来更新控件?只需从 DoWork 调用它们...(当它尝试使用 DoWork 调用的 void 更新数据源时,它会崩溃)。
谢谢阿隆。很好的回复!我需要为我的 WPF 编码打下更好的基础.. :S 这就是我所做的..
private void UpdateStates(Boolean UpdateLeft, Boolean UpdateRight)
{
DataSet ds = new DataSet();
States.State State = new States.State();
if (UpdateRight == true)
{
cState.Dispatcher.Invoke(() =>
{
ds = State.StatesTable();
cState.DataContext = ds.Tables[0].DefaultView;
cState.DisplayMemberPath = ds.Tables[0].Columns[1].ToString();
cState.SelectedValuePath = ds.Tables[0].Columns[0].ToString();
});
}
}
这样可以吗?它告诉我它无法在 system.delegate 类型上转换 lambda,因为它不是委托类型..有什么想法吗?我正在谷歌搜索,但你的帮助对我来说非常有用!谢谢!
I have about 5 voids that query the database and then set the datasource of some controls. As the UI was not frozen while updating the controls, I'm using a BackGroundWorker. Is there a way I can use the same voids to update the controls? just calling them from the DoWork... (it crashs when It tries to update the datasources with the void that the DoWork called).
Thanks Allon. Great reply! I need to get better basis for my WPF coding.. :S this is what I did..
private void UpdateStates(Boolean UpdateLeft, Boolean UpdateRight)
{
DataSet ds = new DataSet();
States.State State = new States.State();
if (UpdateRight == true)
{
cState.Dispatcher.Invoke(() =>
{
ds = State.StatesTable();
cState.DataContext = ds.Tables[0].DefaultView;
cState.DisplayMemberPath = ds.Tables[0].Columns[1].ToString();
cState.SelectedValuePath = ds.Tables[0].Columns[0].ToString();
});
}
}
Is this ok? it tells me that it cant convert lambda on system.delegate type because it is not a delegated type.. any ideas? I'm googling to get but you help will be great for me! Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果要从后台线程与 WPF 控件进行交互,则必须使用其调度程序来封送对 UI 线程的调用。因此,不要直接访问控件:
使用控件的调度程序来运行代码:
If you want to interact with a WPF controls from a background thread, you must use their dispatcher to marshal the call to the UI thread. So instead of accessing the control directly:
Use the control's Dispatcher to run the code: