How do I access the UI thread of a WP7 application?
I am using the following code, if it helps. private void LayoutRoot_Loaded(object sender, RoutedEventArgs e)
{
AcquireNews(l => { listBox1.Items.Add(l开发者_开发问答[0]); });
// Here is where I get an exception saying "Invalid cross-thread access."
}
void AcquireNews(Action<List<object>> callback)
{
var r = HttpWebRequest.Create("http://www.google.com") as HttpWebRequest;
r.BeginGetResponse(result =>
{
var response = r.EndGetResponse(result);
List<object> l = new List<object>();
var s = response.GetResponseStream();
var buffer = new byte[s.Length];
s.Read(buffer, 0, (int)s.Length);
l.Add(System.Text.Encoding.UTF8.GetString(buffer, 0, buffer.Length));
callback(l);
},
null);
}
You can use the Dispatcher for this.
Dispatcher.BeginInvoke( () => { /* Your UI Code - ie Callback() or listbox.items.add */ } );
精彩评论