c# - Add textblock text to favorite list on button click -
i have 2 pages: first mainpage.xaml
, second favoriteslist.xaml
.
in mainpage.xaml
have text block, shows dynamic text automatically.
and have button on mainpage.xaml
.
from want when click on button, text appears on text block should go favorite list in favoriteslist.xaml
page.
if text favorite, text appears on text block should removed favorite list on button click.
so need implement functionality textblock shows dynamically created need know how develop add favorite
functionality.
textblock:
<textblock x:name="stringtextblock" text="" margin="9,-7,0,0" style="{staticresource phonetexttitle1style}" />
button:
<button grid.row="2" x:name="addtofavoritesbutton" content="add" style="{staticresource buttonstyle2}" margin="2" click="addtofavoritesbutton_click"/>
c#
private void addtofavoritesbutton_click(object sender, routedeventargs e) { }
listbox:
<listbox x:name="favoritelistbox" />
i use isolatedstoragesettings store list , compare dynamic text list in isolatedstoragesettings upon button click. on favouriteslist page, set itemsource of listbox list in isolatedstoragesettings.so here steps followed: 1. create model/class set dynamic text being shown on text block
public class favourites { public string mytext { get; set; } }
2. in button click event on mainpage.xaml.cs, first set dynamic text (where ever getting from) text block if need , create list and/or compare
private void addtofavoritesbutton_click(object sender, routedeventargs e) { //your dynamic text set textblock stringtextblock.text = mydynamictext; //set value of text member variable of model/class favourites f = new favourites(); f.mytext = mydynamictext; isolatedstoragesettings settings = isolatedstoragesettings.applicationsettings; /*check if "favouriteslist" key present in isolatedstoragesettings means list had been added. if yes, retrieve list, compare each item dynamic text, add or remove accordingly , replace new list in isolatedstoragesettings same key. */ if (settings.contains("favouriteslist")) { list<favourites> l = (list<favourites>)settings["favouriteslist"]; for(int = 0; <= l.count()-1; i++) { if (l[i].equals(mydynamictext)) { l.removeat(i); settings["favouriteslist"] = l; } else { l.add(f); settings["favouriteslist"] = l; } } } //if no key in isolatedstoragesettings means no data has been added //in list , isolatedstoragesettings. add new data else { list<favourites> l = new list<favourites>(); l.add(f); settings["favouriteslist"] = l; } settings.save(); }
now left show updated list in favouriteslist page. added 'nodata' textblock should visible when there nothing in list. else list displayed. in favouriteslist.xaml
<listbox x:name="favoritelistbox" visibility="collapsed"> <listbox.itemtemplate> <datatemplate> <textblock text="{binding mytext}"/> </datatemplate> </listbox.itemtemplate> </listbox> <textblock name="nodata" text="no data" visibility="collapsed" width="50" height="50"/>
in favouriteslist.xaml.cs
isolatedstoragesettings settings = isolatedstoragesettings.applicationsettings; if (settings.contains("favouriteslist")) { list<favourites> l = (list<favourites>)settings["favouriteslist"]; if(l.count!= 0) { nodata.visibility = system.windows.visibility.collapsed; favoritelistbox.visibility = system.windows.visibility.visible; favoritelistbox.itemssource = l; } } else { favoritelistbox.visibility = system.windows.visibility.collapsed; nodata.visibility = system.windows.visibility.visible; }
i have not tested should work. hope helps!
Comments
Post a Comment