c# - synchronously show and hide controls in windows phone 8 -
in windows phone 8 app want show message before loading items database. after loading want hide message again.
for examle xamlcode this:
<grid name="layoutroot"> <button content="load more..." click="loadmore_clicked"/> <textblock name="loadingmessage" visibility="collapsed" text="loading..." /> <phone:longlistselector itemssource="{binding students}"> <phone:longlistselector.itemtemplate> <datatemplate> <textblock margin="10" text="{binding name}" /> </datatemplate> </phone:longlistselector.itemtemplate> </phone:longlistselector> </grid>
and in c# code trying this:
private void loadmore_clicked(object sender, routedeventargs e) { loadingmessage.visibility = visibility.visible; loadmoreitemsforlonglist(); // loading more items longlistselector's itemsource loadingmessage.visibility = visibility.collapsed; }
the "loadingmessage" textblock never been shown. testing put thread.sleep() in loadmoreitemsforlonglist() function. didn't work either.
my goal show "loadingmessage" textblock before start loading more data longlistselector , hide "loadingmessage" textblock after finishing load of more data. how can that? appreciated.
seems loadmoreitemsforlonglist blocking method. when visibility of loadingmessage set, it's visual apprearance won't updated until control goes of loadmore_clicked (which running in ui thread) , ui can show loadingmessage, before that, it's visibility set collapsed.
you run loadmoreitemsforlonglist in thread:
private void loadmore_clicked(object sender, routedeventargs e) { loadingmessage.visibility = visibility.visible; task.run( () => loadmoreitemsforlonglist() // loading more items longlistselector's itemsource ).continuewith( () => loadingmessage.visibility = visibility.collapsed; ); }
but need aware ui interactions in loadmoreitemsforlonglist need done in ui thread. may use dispatcher:
void loadmoreitemsforlonglist() { // load list db corewindow.getforcurrentthread().dispatcher.current.runasync( coredispatcherpriority.normal, () => { this.lstitems.itemssouce = ...; }); }
but if follow async design patterns , define loadmoreitemsforlonglist async, can instead:
private async void loadmore_clicked(object sender, routedeventargs e) { loadingmessage.visibility = visibility.visible; await loadmoreitemsforlonglistasync(); loadingmessage.visibility = visibility.collapsed; }
this prefered way. , again, still need perfom ui actions of loadmoreitemsforlonglistasync in ui thread.
Comments
Post a Comment