Windows Phone 7: reference page controls in thread-safe way -


here trying do. in wp7 app, loading page has 2 stackpanels. stackpanel1 "collapsed" , stackpanel2 "visible". on load of page, kicking off httpwebrequest , processing begingetresponse asynchronously. @ point want swap visibility of 2 stackpanels. however, since begingetresponse run asynchronously, no longer in ui thread , cannot manipulate these stackpanel controls. if try reference them, of course, "an object reference required non-static field, method, or property 'blah.stackpanel1'"

this makes sense , why.

here things have tried:

  1. delegates, way sliced it, needed static reference controls. fail.
  2. i tried create static reference page class , use reference controls in begingetresponse. compiled, got unauthorizedaccessexception 'invalid cross-thread access.' @ run-time when tried reference controls.
  3. searching , searching , searching.
  4. using deployment.current.dispatcher.begininvoke run on ui thread.

how can statically reference these controls?

or there better way i'm doing?

edit:

here httpwebrequest

if (networkinterface.getisnetworkavailable())         {             httpwebrequest httpwebrequest = httpwebrequest.createhttp("http://urlthatworks.com");             httpwebrequest.method = "get";             httpwebrequest.begingetresponse((asyncresult) =>                 //do processing of return here                  //then here problem                 stackpanel1.visibility = system.windows.visibility.visible;                 stackpanel2.visibility = system.windows.visibility.collapsed;             }, httpwebrequest);          } 

another edit:

and here how tried deployment.current.dispatcher.begininvoke

  httpwebrequest.begingetresponse((asyncresult) =>       //do processing of return here        deployment.current.dispatcher.begininvoke(() =>           {               stackpanel1.visibility = system.windows.visibility.visible;               stackpanel2.visibility = system.windows.visibility.collapsed;           });   }, httpwebrequest); 

you don't want static reference, want thread-safe way of accessing them.
can execute on ui thread by:

deployment.current.dispatcher.begininvoke(()=> somemethod); 

or

deployment.current.dispatcher.begininvoke(()=> {  // code }); 

Comments

Popular posts from this blog

delphi - How to convert bitmaps to video? -

jasper reports - Fixed header in Excel using JasperReports -

python - ('The SQL contains 0 parameter markers, but 50 parameters were supplied', 'HY000') or TypeError: 'tuple' object is not callable -