xaml - Item added to ObservableCollection is successfull but still throws exception -
``i utterly confused on why getting error:
system.nullreferenceexception occurred hresult=-2147467261 message=object reference not set instance of object. source=xamarin.forms.platform.uap stacktrace: @ xamarin.forms.platform.uwp.windowsbaseplatformservices.get_isinvokerequired() @ xamarin.forms.listproxy.oncollectionchanged(object sender, notifycollectionchangedeventargs e) @ xamarin.forms.listproxy.weaknotifyproxy.oncollectionchanged(object sender, notifycollectionchangedeventargs e) @ system.collections.objectmodel.observablecollection
1.oncollectionchanged(notifycollectionchangedeventargs e) @ system.collections.objectmodel.observablecollection
1.insertitem(int32 index, t item) @ system.collections.objectmodel.collection`1.add(t item) @ viewmodels.scanbadgesviewmodel.add(badgescan result) innerexception:
the error results following line:
employeeids.add(badge.employeeid)
note: error observed on xamarin.forms windows 10 universal app (preview).
if comment out listview element inside xaml, no longer receive null exception.
if comment out itemtemplate of listview, still observe null exception.
xaml:
<grid grid.row="4" grid.rowspacing="3" grid.columnspacing="3" backgroundcolor="silver"> <listview itemssource="{binding employeeids}" selecteditem="{binding selectedemployeeid}" backgroundcolor="black"> <listview.itemtemplate> <datatemplate> <viewcell> <viewcell.view> <label text="{binding value}" textcolor="yellow" xalign="start" /> </viewcell.view> </viewcell> </datatemplate> </listview.itemtemplate> </listview> </grid>
viewmodel property:
observablecollection<employeeid> _employeeids = new observablecollection<employeeid>(); public observablecollection<employeeid> employeeids { { return _employeeids; } set { if (_employeeids != value) { _employeeids = value; onnotifypropertychanged(); } } }
entities:
public class employeeid { public employeeid(string employeeid) { value = employeeid; } public string value { get; } } public class badgescan { public badgescan(string employeeid) { employeeid = new employeeid(employeeid); } public badgescan(badgescan source, predicate<string> validate) : this(source.employeeid.value) { isvalid = validate.invoke(source.employeeid.value); } public employeeid employeeid { get; } public bool isvalid { get; } }
update
this line of code alters behavior of observablecollection.add operation:
var administeredscan = new badgescan(result, validate);
the line creates copy of object.
var validate = dependencymanager.resolve(typeof(predicate<string>)) predicate<string>; var administeredscan = new badgescan(result, validate); var canadd = canadd(administeredscan) && scanmode == entities.scanmode.add; if (canadd) add(administeredscan); break;
this still throws exception though item added collection: add(administeredscan);
however, succeeds:
var result = obj badgescan; add(result);
so creating copy of object add observable fails. adding original object succeeds.
this xamarin.forms bug in regards windows universal platform (i.e. windows 10).
instead of invoking add operation on observablecollection ui data-bound to, create new observablecollection each add operation , pass in collection within constructor.
workaround:
_employeeidshack.add(administeredscan.employeeid); employeeids = new observablecollection<employeeid>(_employeeidshack);
Comments
Post a Comment