WP7 - 访问UI线程? [英] WP7 - accessing UI thread?
本文介绍了WP7 - 访问UI线程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我如何访问WP7应用程序的UI线程?结果
我使用下面的code,如果有帮助。
私人无效LayoutRoot_Loaded(对象发件人,RoutedEventArgs E)
{
AcquireNews(L => {listBox1.Items.Add(L [0]);});
//这里就是我得到一个异常说无效的跨线程访问。
} 无效AcquireNews(动作<名单,LT;对象>>回调)
{
变种R = HttpWebRequest.Create(http://www.google.com)作为HttpWebRequest的; r.BeginGetResponse(结果=>
{
VAR响应= r.EndGetResponse(结果); 清单<对象> L =新的List<对象>(); 变种S = response.GetResponseStream(); VAR缓冲=新的字节[s.Length] s.Read(缓冲液,0,(int)的s.Length); l.Add(System.Text.Encoding.UTF8.GetString(缓冲液,0,buffer.Length)); 回调(升);
},
空值);
}
解决方案
您可以使用调度这一点。
Dispatcher.BeginInvoke(()=> {/ *你的UI code - 即回调()或listbox.items.add * /});
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 */ } );
这篇关于WP7 - 访问UI线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文