1
如何訪問WP7應用程序的UI線程?
我使用下面的代碼,如果有幫助。WP7 - 訪問UI線程?
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);
}
非常感謝!它終於有效! :D – Vercas 2010-12-11 00:09:54
yw :) ../15char – 2010-12-11 00:54:37
感謝您的解決方案:) 請記住使用Deployment.Current.Dispatcher.BeginInvoke 而不是System.Threading(就像我做的那樣:)) – 2012-01-12 19:39:20