【问题标题】:Windows phone - httpwebrequest - passing ref variables to callbackWindows 手机 - httpwebrequest - 将 ref 变量传递给回调
【发布时间】: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


    【解决方案1】:

    我会使用async/await。生活可以更轻松...

    HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(new Uri("myurl"));
    var response = await request.GetResponseAsync();
    
    using (StreamReader streamReader = new StreamReader(response.GetResponseStream()))
    {
        string text = streamReader.ReadToEnd();
        // Updating UI is safe. Do it whichever way you want!!!
    }
    

    编辑

    你也可以使用HttpClient

    using (HttpClient client = new HttpClient())
    {
        string text = await client.GetStringAsync("myurl");    
        //update UI here
    }
    

    【讨论】:

    • 找不到方法GetResponseAsync..我应该添加什么使用?它还说我只能在具有异步修饰符的方法中使用“等待”,并且关于返回 Task 对象..
    • 您能否详细说明在何处使用此代码,据我所知,我需要带有关键字 async 的方法?
    • @lenden 只需将async 添加到您的方法声明中(如果它是无效的)。如果您要返回的内容,请将声明从 X 更改为 Task&lt;X&gt;
    • 仅在 8.1 中不支持 Phone 8。 matthiasshapiro.com/2012/12/10/…
    • 哇.. 怎么办?我在 NuGet 中找到了 HttpWebRequest.Async.Extensions,有帮助吗?
    【解决方案2】:
    public static void DoOnUIThread(some parameters)
    {
      // You can use parameters now.  Obviously "some" is not a type, but you would replace this with the type you're using for your parameters.
    }
    
    public static void AddEventTypeToList(some parameters)
    {
        // Change Action<Type> to use the type of the parameter you want
           Deployment.Current.Dispatcher.BeginInvoke(new Action<some>(DoOnUIThread));
    }   
    

    【讨论】:

    • 我觉得我写错了..看问题更新很快
    猜你喜欢
    • 2011-11-13
    • 1970-01-01
    • 1970-01-01
    • 2016-01-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-23
    • 2019-10-08
    相关资源
    最近更新 更多