c# - Application crashing when adding item to listbox -
when try add listbox, application close.
this have far. line causing close is: listbox1.items.add(term1)
using system; using system.io; using system.collections; using system.collections.generic; using system.linq; using system.net; using system.windows; using system.windows.controls; using system.windows.documents; using system.windows.input; using system.windows.media; using system.windows.media.animation; using system.windows.shapes; using microsoft.phone.controls; namespace phoneapp1 { public partial class mainpage : phoneapplicationpage { // constructor public class item { public string itemline1 { get; set; } public string itemline2 { get; set; } } public mainpage() { initializecomponent(); list<item> list = new list<item>(); item item = new item(); item.itemline1 = "item11"; item.itemline2 = "item12"; list.add(item); item = new item(); item.itemline1 = "item21"; item.itemline2 = "item22"; list.add(item); item = new item(); item.itemline1 = "item31"; item.itemline2 = "item32"; list.add(item); dispatcher.begininvoke(new action(() => listbox1.itemssource = list )); webclient wc = new webclient(); wc.downloadstringcompleted += new downloadstringcompletedeventhandler (wc_downloadstringcompleted); wc.downloadstringasync(new uri("http://www.usi.edu/webservices/iphone/usiinfoterms.xml")); } void wc_downloadstringcompleted(object sender, downloadstringcompletedeventargs e) { applicationtitle.text = e.result; string terms = applicationtitle.text; applicationtitle.text = "course catalog"; string term1 = terms.substring(terms.indexof("value"+7),terms.indexof("/value")); listbox1.items.add(term1); } private void listbox1_selectionchanged(object sender, selectionchangedeventargs e) { if (sender != null) pagetitle.text = sender.tostring(); if (e != null) pagetitle.text = e.addeditems.count.tostring(); ienumerator ie = e.addeditems.getenumerator(); ie.movenext(); if (e != null) applicationtitle.text = ie.current.tostring(); } } }
you've set itemssource accept type of item, you're adding string it. turn string item before adding it, matches expected type of itemsource.
Comments
Post a Comment