Mahdi Salah
Mahdi Salah

Reputation: 189

Binding ObservableCollection take much time in fact the window take much time to be displayed

I'm using MVVM light Framework with WPF and I have a DataGrid that contain all the customers loaded from my SQLite database, But it take too much time to display the Window so if any one can help me for I can dislpay the window and load the DataGrid separately.I think that the Window is taking time because of the DataGrid Binding.

 public ObservableCollection<CustumerModel> customerList
    {
        get
        {
            _customerList = new ObservableCollection<CustumerModel>();
            IList<CustumerModel> listCustomer = RemplireListCustomer();
            _customerList = new ObservableCollection<CustumerModel>(listCustomer);
            return _customerList;
        } 

the method RemplireListCustomer

     private IList<CustumerModel> RemplireListCustomer()
    {
        IList<CustumerModel> listCustomer = new List<CustumerModel>();
        foreach (var c in _customerService.GetAllCustomers())
        {
            listCustomer.Add((CustumerModel)c);
        }
        return listCustomer;
    }

Upvotes: 0

Views: 570

Answers (1)

Mikko Viitala
Mikko Viitala

Reputation: 8404

You could load your data async by starting a new Task in e.g. your class' constructor.

public class YourClass
{
    public YourClass()
    {
        TaskEx.Run(() =>
        {
            var listCustomer = RemplireListCustomer();
            CustomerList = new ObservableCollection<CustumerModel>(listCustomer);
        });
    }

    public ObservableCollection<CustumerModel> CustomerList { get; private set; }
}

And just maybe you do not have to iterate over all customers returned by your service using foreach, just return the collection _customerService.GetAllCustomers()?

Upvotes: 1

Related Questions