c# - An unhandled exception occured when updating ObservableCollection list -
im trying load data webservice sfdatagrid using parameters onnavigatedto
first loads dummy row grid because can´t data onnavigatedto yet runs onnavigatedto , gets data webservice , gose adding data observablecollection grid has itemssource set to
using prism.commands; using prism.mvvm; using prism.navigation; using system; using system.collections.generic; using system.collections.objectmodel; using system.componentmodel; using system.linq; namespace prismunityapp2.viewmodels { public class hovedsideviewmodel : bindablebase, inotifypropertychanged , inavigationaware { mobileservice.vaksserviceclient ws = new mobileservice.vaksserviceclient(); inavigationservice _navigationservice; public event propertychangedeventhandler propertychanged; private datetime selecteddate; public datetime selecteddate { { return selecteddate; } set { if (selecteddate != value) { selecteddate = value; if (propertychanged != null) { propertychanged(this, new propertychangedeventargs("selecteddate")); } } } } private datetime minimumselecteddate; public datetime minimumselecteddate { { return minimumselecteddate; } set { if (minimumselecteddate != value) { minimumselecteddate = value; if (propertychanged != null) { propertychanged(this, new propertychangedeventargs("minimumselecteddate")); } } } } public delegatecommand navigatetest { get; private set; } public delegatecommand navigatetobestilling { get; private set; } public string latitude { get; set; } private string _longitude; public string longitude { { return _longitude; } set { _longitude = value; ws.gettoemmedatasasync(latitude, longitude, datetime.today.adddays(-7)); // needs set 1 of november day data } } public hovedsideviewmodel(inavigationservice navigationservice) { try { toemmeliste = new observablecollection<toemningdata>(); } catch (exception ex) { throw; } setrowdata(); minimumselecteddate = datetime.now; selecteddate = datetime.now.adddays(1); _navigationservice = navigationservice; navigatetest = new delegatecommand(navigate); navigatetobestilling = new delegatecommand(_navigatetobestilling); } private void _navigatetobestilling() { _navigationservice.navigateasync("bestilling"); } private void navigate() { _navigationservice.navigateasync("scanner"); } public void onnavigatedfrom(navigationparameters parameters) { } public void onnavigatedto(navigationparameters parameters) { latitude = parameters["lat"].tostring(); // if phone inst near latitude "55,7070631" , longitude "12,4235393" remove part gets data parameters longitude = parameters["lon"].tostring(); // , use 2 lines under //latitude = "55,7070631"; //longitude = "12,4235393"; } public void setrowdata() { ws.gettoemmedatascompleted += ws_gettoemmedatascompleted; ws.gettoemmedatasasync(latitude, longitude, datetime.today.adddays(-7)); // needs set 1 of november day data } private void ws_gettoemmedatascompleted(object sender, mobileservice.gettoemmedatascompletedeventargs e) { int numofrows = 0; try { numofrows = e.result.count(); // awdawd } catch (exception) { numofrows = 0; } //toemmeliste.clear(); if (numofrows > 0) { foreach (var toemming in e.result) { toemningdata _toemningdata = new toemningdata(); _toemningdata.fraktion = toemming.fraktion; _toemningdata.toemmeid = toemming.toemmeid; _toemningdata.type = toemming.type; _toemningdata.vaegt = toemming.weight; toemmeliste.add(_toemningdata); // failes when trys add new row // unhandled exception occured. } } else // if there isnt data set 1 row saying there isnt data { toemningdata _toemningdata = new toemningdata(); _toemningdata.fraktion = "igen data"; _toemningdata.toemmeid = -1; _toemningdata.type = "igen data"; _toemningdata.vaegt = "igen data"; toemmeliste.add(_toemningdata); } } private observablecollection<toemningdata> toemmeliste; public observablecollection<toemningdata> toemmeliste { { return toemmeliste; } set { this.toemmeliste = value; } } } }
xaml
<?xml version="1.0" encoding="utf-8" ?> <contentpage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:prism="clr-namespace:prism.mvvm;assembly=prism.forms" prism:viewmodellocator.autowireviewmodel="true" xmlns:sfgrid="clr-namespace:syncfusion.sfdatagrid.xforms;assembly=syncfusion.sfdatagrid.xforms" x:class="prismunityapp2.views.hovedside" xmlns:sys="clr-namespace:system;assembly=mscorlib" title="test"> <contentpage.content> <grid> <grid.rowdefinitions> <rowdefinition height="20" /> <rowdefinition height="50"/> <rowdefinition height="*" /> <rowdefinition height="50" /> <rowdefinition height="50" /> </grid.rowdefinitions> <grid.columndefinitions> <columndefinition width="*"/> <columndefinition width="*"/> <columndefinition width="*"/> </grid.columndefinitions> <datepicker date="{binding selecteddate,mode=twoway}" minimumdate="{binding minimumselecteddate,mode=twoway}" grid.row="1" grid.column="0" grid.columnspan="3" horizontaloptions="center"> <datepicker.format>yyyy-mm-dd</datepicker.format> </datepicker> <sfgrid:sfdatagrid x:name="datagrid" autogeneratecolumns="false" columnsizer="star" selectedindex="1" selectionmode="single" itemssource="{binding toemmeliste}" allowsorting="true" grid.row="2" grid.column="0" grid.columnspan="3" > <sfgrid:sfdatagrid.columns x:typearguments="syncfusion:columns"> <sfgrid:gridtextcolumn headertext="fraktion" mappingname="fraktion" /> <!--fraktion--> <sfgrid:gridtextcolumn headertext="type" mappingname="type" /> <!--type--> <sfgrid:gridtextcolumn headertext="vaegt" mappingname="vaegt" /> <!--vægt--> </sfgrid:sfdatagrid.columns> </sfgrid:sfdatagrid> <button text="scan" fontsize="24" grid.row="3" grid.column="0" command="{binding navigatetest}"/> <button text="mail" fontsize="24" grid.row="3" grid.column="2"/> <button text="manuel" fontsize="24" grid.row="4" grid.column="0" grid.columnspan="3" horizontaloptions="center" command="{binding navigatetobestilling}"/> </grid> </contentpage.content> </contentpage>
when gets in ws_gettoemmedatascompleted after getting data webservice stops @ toemmeliste.add(_toemningdata); in
foreach (var toemming in e.result) { toemningdata _toemningdata = new toemningdata(); _toemningdata.fraktion = toemming.fraktion; _toemningdata.toemmeid = toemming.toemmeid; _toemningdata.type = toemming.type; _toemningdata.vaegt = toemming.weight; toemmeliste.add(_toemningdata); // failes when trys add new row // unhandled exception occured. }
and says unhandled exception occured.
any appreciated
im using visual studio 2015 community, xamarin, prism v6.2.0 xam.plugin.geolocator v3.0.4 , syncfusion running on sony xperia z3 android version 6.0.1 , using api 23
the solution went with
using prism.commands; using prism.mvvm; using prism.navigation; using prismunityapp2.proxyclient; using system; using system.collections.generic; using system.collections.objectmodel; using system.componentmodel; using system.linq; using system.servicemodel; using system.threading.tasks; namespace prismunityapp2.viewmodels { public class hovedsideviewmodel : bindablebase, inotifypropertychanged, inavigationaware { mobileservice.vaksserviceclient ws = new mobileservice.vaksserviceclient(); inavigationservice _navigationservice; public event propertychangedeventhandler propertychanged; private datetime selecteddate; public datetime selecteddate { { return selecteddate; } set { if (selecteddate != value) { selecteddate = value; if (propertychanged != null) { propertychanged(this, new propertychangedeventargs("selecteddate")); } } } } private datetime minimumselecteddate; public datetime minimumselecteddate { { return minimumselecteddate; } set { if (minimumselecteddate != value) { minimumselecteddate = value; if (propertychanged != null) { propertychanged(this, new propertychangedeventargs("minimumselecteddate")); } } } } public delegatecommand navigatetest { get; private set; } public delegatecommand navigatetobestilling { get; private set; } public string latitude { get; set; } private string _longitude; public string longitude { { return _longitude; } set { _longitude = value; } } public hovedsideviewmodel(inavigationservice navigationservice) { setrowdata(); // initialize grid minimumselecteddate = datetime.now; // 1 revmoved later. fine can go , watch not edite selecteddate = datetime.now.adddays(1); _navigationservice = navigationservice; navigatetest = new delegatecommand(navigate); navigatetobestilling = new delegatecommand(_navigatetobestilling); } private void _navigatetobestilling() { _navigationservice.navigateasync("bestilling"); } private void navigate() { _navigationservice.navigateasync("scanner"); } public void onnavigatedfrom(navigationparameters parameters) { } public async void onnavigatedto(navigationparameters parameters) { xamarin.forms.device.begininvokeonmainthread(async () => { latitude = parameters["lat"].tostring(); // if phone inst near latitude "55,7070631" , longitude "12,4235393" remove part gets data parameters longitude = parameters["lon"].tostring(); // , use 2 lines under //latitude = "55,7070631"; //longitude = "12,4235393"; var ggg = await callservice(new mobileservice.toemning { latitude = latitude, longitude = longitude, date = new datetime(2016, 11, 1) }); // date set day there data var justatest = 0; toemmeliste.clear(); foreach (var toemming in ggg) { toemningdata _toemningdata = new toemningdata(); _toemningdata.fraktion = toemming.fraktion; _toemningdata.toemmeid = toemming.toemmeid; _toemningdata.type = toemming.type; _toemningdata.vaegt = toemming.weight; toemmeliste.add(_toemningdata); } }); } mobileservice.toemning towcfservicetodoitem(mobileservice.toemning item) { return new mobileservice.toemning { fraktion = item.fraktion, toemmeid = item.toemmeid, type = item.type, date = item.date, latitude = item.latitude, longitude = item.longitude, weight = item.weight }; } public async task<observablecollection<mobileservice.toemning>> callservice(mobileservice.toemning item) { var todoitem = towcfservicetodoitem(item); mobileservice.vaksserviceclient client = new mobileservice.vaksserviceclient(new basichttpbinding(), new endpointaddress("http://vf-kssweb2/vaks2svc/vaksservice.svc?singlewsdl")); var t = task<observablecollection<mobileservice.toemning>>.factory.fromasync( ((mobileservice.ivaksservice)client.innerchannel).begingettoemmedatas, ((mobileservice.ivaksservice)client.innerchannel).endgettoemmedatas, todoitem, taskcreationoptions.none); return await t; } public void setrowdata() { try { toemmeliste = new observablecollection<toemningdata>(); } catch (exception ex) { throw; } toemningdata _toemningdata = new toemningdata(); _toemningdata.fraktion = "igen data"; _toemningdata.toemmeid = -1; _toemningdata.type = "igen data"; _toemningdata.vaegt = "igen data"; toemmeliste.add(_toemningdata); } private observablecollection<toemningdata> toemmeliste; public observablecollection<toemningdata> toemmeliste { { return toemmeliste; } set { this.toemmeliste = value; } } } }
you're not allowed change observablecollection
thread 1 created (normally ui thread). have marshall add
operations thread, preferably using async
/await
:
private async task filllist() { // on ui thread here... // leaving wait i/o operations or cpu-bound work on threadpool thread var data = await _server.fetchdata(); // here on ui thread , update list foreach (var item in data) transformitemandaddittothelist( item ); }
Comments
Post a Comment