有一个类似的问题(How to pass the UI Dispatcher to the ViewModel 有人给我提供一些示例或完整代码。我是 MVVM 新手,所以我不明白。
private async void BindMatchGridinAsync()
{
await BindMatchGridAsync();
}
async Task<ObservableCollection<EfesBet.DataContract.GetMatchDetailsDC>> BindMatchGridAsync()
{
await Task.Run(() =>
{
BindMatchGrid();
});
return null;
}
void BindMatchGrid()
{
BindMatchGridDel bindDel = new BindMatchGridDel(BindMatchGridData);
matchList = new List<GetMatchDetailsDC>();
matchList = proxy.GetMatch().ToList();
dataGridParent.Dispatcher.BeginInvoke(bindDel, null);
}
public delegate void BindMatchGridDel();
void BindMatchGridData()
{
dataGridParent.ItemsSource = matchList;
}
现在,在这段代码 BindMatchGridinAsync() 中,我已将代码的构造函数放入文件(.cs 文件)后面。它提供了适当的异步操作。但我需要在 ViewModel 中实现相同的功能。请建议我如何实现它。 预先感谢您。
最佳答案
您像 WinForms 开发人员一样思考这个问题,其中每个控件都可以位于单独的线程上。
在 WPF 中,有一个 UI 线程。该线程称为调度程序。因此每个控件都将使用相同的调度程序。
如上所述:
Application.Current.Dispatcher.BeginInvoke(new Action(DoSomething),null);
完全可以接受,并且可以与您的所有控件一起使用,因为所有控件都使用应用程序调度程序。
关于wpf - 如何将 View 中控件的 UI 调度程序传递给 ViewModel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18532497/