i have service holds list of objects.
i have viewmodel property referencing list:
public myviewmodel(imyservice myservice) { mylist = myservice.thelist; } public list<object> mylist { { return getvalue...etc
this viewmodel view named myview, displayed in tabitem, in tabcontrol, none of wich program's first view. myview contains combobox itemssource binded mylist.
when program launched, myservice.thelist
null. later, loaded. then, going in tab displaying myview shows empty combobox. switching tabitem , coming fires onpropertychanged, , combobox populated.
why onpropertychanged fired second time view displayed?
if service lazy-loads (or @ least @ later stage), should implement events on service inform listeners data has become available. should on view model.
note implementation prevents memory leaks (since handles both subscription , unsubscription service), lot of people forget when implementing view models.
private readonly imyservice _myservice; public myviewmodel(imyservice myservice) { argument.isnotnull(() => myservice); _myservice = myservice; } public list<object> mylist { get; private set; } protected virtual async task initialyzeasync() { _myservice.receiveddata += onmyservicereceiveddata; updatedata(); } protected virtual async task closeasync() { _myservice.receiveddata -= onmyservicereceiveddata; } private void onmyservicereceiveddata(object sender, eventargs e) { updatedata(); } private void updatedata() { mylist = new list<object>(myservice.thelist); }
Comments
Post a Comment