c# - Cant Find Control Windows Phone 8.1 UAP -
i trying find control under xaml in hub control of windows phone 8.1. may case method works fine on desktop have testest in wpf before porting windows phone , may not work same.
<grid> <hub header="lists" name="mainhub" > <hubsection minwidth="600" name="lattestlists" header="new lists"> <datatemplate> <grid> <listbox background="transparent" margin="6" height="auto" borderthickness="2" maxheight="580" grid.row="1" x:name="listboxobj" > <listbox.itemtemplate> <datatemplate> <grid width="350" > <border margin="5" borderbrush="white" borderthickness="1"> <grid> <grid.rowdefinitions> <rowdefinition height="auto"/> <rowdefinition height="auto"/> </grid.rowdefinitions> <textblock margin="5,0,0,0" grid.row="0" x:name="nametxt" textwrapping="wrap" text="{binding name}" fontsize="28" foreground="white"/> <textblock grid.row="0" text=">" fontsize="28" horizontalalignment="right" verticalalignment="center" foreground="white"/> <textblock margin="5,0,0,0" grid.row="1" x:name="phonetxt" textwrapping="wrap" foreground="white" fontsize="18" text="{binding phonenumber}" /> <textblock horizontalalignment="right" margin="0,0,35,0" grid.row="3" x:name="createddatetxt" foreground="white" fontsize="18" textwrapping="wrap" text="{binding creationdate}" /> </grid> </border> </grid> </datatemplate> </listbox.itemtemplate> </listbox> </grid> </datatemplate> </hubsection> <hubsection header="tech" isheaderinteractive="true" background="#222222" minwidth="250"> <datatemplate> <stackpanel> <textblock text="tech news goes here." style="{themeresource bodytextblockstyle}" /> <textblock text="click header go tech page." style="{themeresource bodytextblockstyle}" /> </stackpanel> </datatemplate> </hubsection> <hubsection header="sports" isheaderinteractive="true" background="#444444" minwidth="250"> <datatemplate> <stackpanel> <textblock text="sports news goes here." style="{themeresource bodytextblockstyle}" /> <textblock text="click header go sports page." style="{themeresource bodytextblockstyle}" /> </stackpanel> </datatemplate> </hubsection> </hub> </grid>
i trying use following method find lsitbox not working
listbox listboxobjc = findchildcontrol<listbox>(this, "listboxobj") listbox; listboxobjc.itemssource = db_contactlist.orderbydescending(i => i.id).tolist();//binding db data listbox , latest contact id can display first.
this method findchildcontrol
private dependencyobject findchildcontrol<t>(dependencyobject control, string ctrlname) { int childnumber = visualtreehelper.getchildrencount(control); (int = 0; < childnumber; i++) { dependencyobject child = visualtreehelper.getchild(control, i); frameworkelement fe = child frameworkelement; // not framework element or null if (fe == null) return null; if (child t && fe.name == ctrlname) { // found control return return child; } else { // not found - search children dependencyobject nextlevel = findchildcontrol<t>(child, ctrlname); if (nextlevel != null) return nextlevel; } } return null; }
edit debuged code , showing control null though have done same way would
i have replace method find children couple of times during different versions of windows phone / windows runtime 1 being reliable. in case you're not able spot mistake try 1 , see if yields better results:
public t findelementbyname<t>(dependencyobject element, string schildname) t : frameworkelement { t childelement = null; var nchildcount = visualtreehelper.getchildrencount(element); (int = 0; < nchildcount; i++) { frameworkelement child = visualtreehelper.getchild(element, i) frameworkelement; if (child == null) continue; if (child t && child.name.equals(schildname)) { childelement = (t)child; break; } childelement = findelementbyname<t>(child, schildname); if (childelement != null) break; } return childelement; }
also when using method shortly after creating / loading content call updatelayout beforehand:
this.updatelayout();
but careful - not 1 of performance-friendliest methods (see also: https://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.uielement.updatelayout).
Comments
Post a Comment