【发布时间】:2014-08-12 20:21:22
【问题描述】:
我正在从 Windows Phone 应用程序发送 HttpWebRequest:
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(new Uri("myurl"));
request.BeginGetResponse(MyProcessor, request);
这里是回调:
public void MyProcessor(IAsyncResult asynchronousResult)
{
HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;
HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asynchronousResult);
using (StreamReader streamReader = new StreamReader(response.GetResponseStream()))
{
string text = streamReader.ReadToEnd();
// passing some arguments to special class, that generates ui elements
}
}
这里是生成器类的方法
public static void AddEventTypeToList(some parameters)
{
Deployment.Current.Dispatcher.BeginInvoke(() =>
{
//xaml generating
});
}
所以。我需要使用 Dispatcher.BeginInvoke,否则我会得到“无效的跨线程访问”。
但是。我还需要传递对某些参数的引用...并且我收到错误“您不能在匿名方法、lambda 表达式或查询表达式中使用关键字 ref 或 out 用户参数。
怎么办?我通常需要使用 HttpWebRequest(因为我需要使用状态码并且它会阻止我的 WebClient)并将对某些 ui 元素的引用传递给回调中的特殊 uigenerator 类
我尝试了 Mangist 的答案,但我认为有一些错误:
public static void AddEventTypeToList(EventType ev, ListBox mainListBox, ref List<Grid> inds)
{
Deployment.Current.Dispatcher.BeginInvoke(new Action<EventType, ListBox, List<Grid>>(DoOnUIThread));
}
生成:
public static void DoOnUIThread(EventType ev, ListBox mainListBox, List<Grid> inds)
{
Grid gr = new Grid();
//....
}
这个方法根本没有到达断点
【问题讨论】:
标签: c# windows-phone-8 httpwebrequest dispatcher ref