I have a WPF application, in which I'd like to bind a collection to a combobox using an asynchronous method :
<ComboBox Margin="2,0,5,0" Width="178" ItemsSource="{Binding Animateur}" DisplayMemberPath="nom" SelectedIndex="0" />
the viewmodel class
_service.GetAnimateur((item, error) =>
{
if (error != null)
{
// TODO : traitement d'erreur
}
else
{
_Animateur.Clear();
item.ForEach(Elem =>
{
_Animateur.Add(Elem);
});
}
});
the Asynchrounous method :
public async void GetAnimateur(Action<List<fiche>, Exception> callback)
{
try
{
Task<List<fiche>> data = (Task<List<fiche>>)Task.Run(
() =>
{
DataEntities _db = new DataEntities();
var dpcs = _db.fiche;
return new List<fiche>(dpcs);
});
var result = await data;
callback(result, null);
}
catch (Exception ex)
{
callback(null, ex);
}
}
I have 20128 items in the table fiche
, the problem is that GetAnimateur
takes a lot of time to fill the combobox.
- What are the errors that I commited in this code?
- How can I improve it?
// TODO : traitement d'erreur
? Are you sure this code is complete? \$\endgroup\$ – RubberDuck Feb 14 '15 at 11:14