Here's what I'm trying to do:
public class EmployeeService
{
public void GetEmployeesAsyc(Action<IEnumerable<Employees>> callback)
{
ThreadPool.QueueUserWorkItem(x => {
var employees = //...fetch employees ...//
callback(employees);
});
}
}
public class ViewModel
{
private EmployeeService _employeeService;
private bool _isLoaded;
public ViewModel()
{
_employeeService = new EmployeeService();
EmployeeList = new ObservableCollection<Employee>();
}
public ObservableCollection<Employee> EmployeeList { get; set; }
public void LoadData()
{
if(_isLoaded) return;
_employeeService.GetEmployeesAsync(employees =>
{
EmployeeList.Clear();
employees.ForEach(employee => EmployeeList.Add(employee));
});
_isLoaded = true;
}
}
public partial class View : PhoneApplicationPage
{
private ViewModel _vm;
public View()
{
InitializeComponent();
_vm = new ViewModel();
this.Loaded += (sender, e) => _vm.LoadData();
}
}
The problem here is that this throws a cross-thread exception:
_employeeService.GetEmployeesAsync(employees =>
{
EmployeeList.Clear();
employees.ForEach(employee => EmployeeList.Add(employee));
});
I can fix it by wrapping it like this:
_employeeService.GetEmployeesAsync(employees =>
{
Dispatcher.BeginInvoke(() =>
{
EmployeeList.Clear();
employees.ForEach(employee => EmployeeList.Add(employee));
});
});
but I'm not sure if that's the right way to go about it. Any suggestions?