【问题标题】:Post method never returns in Xamarin Forms iOSPost 方法永远不会在 Xamarin Forms iOS 中返回
【发布时间】:2016-07-04 10:46:00
【问题描述】:

我正在使用下面的代码 sn-p:

public async Task<LoginUser[]> GetUserAsync()
{
    var client = new System.Net.Http.HttpClient();
    client.BaseAddress = 
        new Uri("http://developer-platform.com/dutyfreebuzz/web_services/Login");
    StringContent str = new StringContent("username=admin&password=12345", Encoding.UTF8, "application/x-www-form-urlencoded");
    var response = await client.PostAsync(new Uri("http://developer-platform.com/dutyfreebuzz/web_services/Login/loginJSON"), str);
    var loginJson = await response.Content.ReadAsStringAsync();
    Rootobject rootobject = new Rootobject();
    if (loginJson != "")
    {
        //rootobject = JsonConvert.DeserializeObject<Rootobject>(loginJson);
    }
    //return rootobject.loginusers;
}

我已经为这个函数设置了断点。在执行期间,调试器不会移动到 PostAsync 调用,它正在移出函数。

【问题讨论】:

  • 如果在 iOS 上启用了链接器,则在可移植类库中的 async/await 代码中设置断点目前存在一个已知问题:developer.xamarin.com/releases/vs/xamarin.vs_4/xamarin.vs_4.1 所以你可能会遇到这个问题。有一种解决方法,您可以禁用 iOS 项目的链接。你可能会打那个吗?
  • @Cheesebaron:是的,我已经链接到 IOS。那么我应该如何检查响应。我正在我的 mac 机器上使用 xamarin studio 进行开发。
  • 您可以打印到控制台。另外,如果您在 PostAsync 中使用完整的 URL,我认为您不需要设置 BaseAddress。但是,我认为这不是问题。
  • @Cheesebaron:我通过在响应行代码上设置断点解决了这个问题。但是调试器需要几分钟才能捕捉到。是的,我忘记评论了,我不需要 BaseAddress。
  • @Cheesebaron - 我已经总结了你在 cmets 中提供的答案,如果还有什么要补充的,请编辑或更新。

标签: c# xamarin xamarin.forms


【解决方案1】:

这个答案是 Cheesebaron 和 OP 对 cme​​ts 的总结,因为 cmets 中提供了解决方案。

如果在 iOS 上启用了链接器,则在可移植类库中的 async/await 代码中设置断点目前存在一个已知问题。

41836:对于 iOS 应用程序,PCL 项目中异步方法中的断点 如果启用链接器,则不会命中。部分临时解决方法: 将项目属性 > iOS 构建 > 链接器行为设置为不链接。 (对于设备构建,不幸的是,这种解决方法确实会创建 .app 包要大得多,并且会延长部署时间。)

作为一种解决方法,在WriteLine() 中设置断点:

System.Diagnostics.Debug.WriteLine(response);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-15
    • 2011-07-25
    • 1970-01-01
    • 2012-10-02
    • 2013-02-18
    相关资源
    最近更新 更多