c# - XAML custom collection binding async collection update -
i have custom class inheriting observablecollection
, inotifypropertychanged
(i.e. custom class has properties) serves collection<t>
t
also inherits inotifypropertychanged
:
public class customcollection<t> : observablecollection<t>, inotifypropertychanged t: inotifypropertychanged { private string _name; public string name { { return _name; } set { if (_name != value) { _name = value; notifypropertychanged("name"); } } } private int _total; public int total { { return _total; } set { if (_total != value) { _total = value; notifypropertychanged("total"); } } } public event propertychangedeventhandler propertychanged; private void notifypropertychanged(string propertyname) { propertychangedeventhandler handler = propertychanged; if (null != handler) { handler(this, new propertychangedeventargs(propertyname)); } }
and t
item class:
public class dataitem : inotifypropertychanged { private string _fname; public string fname { { return _fname; } set { if (value != _fname) { _fname = value; notifypropertychanged("fname"); } } } private int_value; public int value { { return _value; } set { if (value != _value) { _value = value; notifypropertychanged("value"); } } } public event propertychangedeventhandler propertychanged; private void notifypropertychanged(string propertyname) { propertychangedeventhandler handler = propertychanged; if (null != handler) { handler(this, new propertychangedeventargs(propertyname)); } } }
and viewmodel
:
public class viewmodel : viewmodelbase { private readonly iservice _dataservice; private bool _isloading; public bool isloading { { return _isloading; } private set { _isloading = value; raisepropertychanged("isloading"); } } private customcollection<dataitem> _items; public customcollection<dataitem> items { { return _items; } set { _items= value; raisepropertychanged("items"); } } public viewmodel(iservice dataservice) { _dataservice = dataservice; } public void refresh() { if (!this.isloading) { this.isloading = true; _dataservice.refreshdata( this, (error) => { if (error != null) { return; } if (!isindesignmode) this.isloading = false; } ); } } public void getdata() { if (games == null) { games = new customcollection<dataitem>(); } else { games.clear(); } if (!this.isloading) { this.isloading = true; _dataservice.getdata( this, (error) => { if (error != null) { return; } if (!isindesignmode) this.isloading = false; } ); } }
and have bound customcollection<t>
control in view (xaml). works fine initially, upon navigating page, viewmodel
calls dataservice
retrieve data , populate customcollection<t>
. however, when refreshing data, view not updated until all data has been iterated on , refreshed/updated!
here code refresh/updated (keep in mind, i'm retrieving data via web service, , purposes of testing have manually updated value
property in dataitem
@ each passover of customcollection<t>
):
public async refreshdata(viewmodel model, action<exception> callback) { if (model.items == null) return; // ... retrieve data web service here (omitted) ... foreach (dataitem item in retrieveditems) { // loop each item in retrieved items dataitem newitem = new dataitem() { fname = item.fname, value = item.value }; if (model.items.contains(newitem)) { // override .equals in customcollection<t> allows comparison fname property model.items[model.items.indexof(newitem)].value += 10; // manual update } else { model.items.add(newitem); } system.threading.thread.sleep(1000); // 1 second pause "see" each item updated sequentially... } callback(null); }
so in summary, how can make updating value
of dataitem
instantly reflect in view, given current setup of customcollection<dateitem>
? async
perhaps? mean, when sleep(1000)
gets called, ui not hang, maybe has it?
any ideas on how fix this? might have guessed, issue present when first retrieving data (but barely noticeable data retrieved/processed during navigation view).
note: i'm using mvvmlight toolkit.
thanks.
Comments
Post a Comment