Xamarin Async和Await:UI线程被阻止

时间:2016-05-27 06:17:39

标签: web-services asynchronous xamarin async-await xamarin.forms

我的项目中有这个架构,有时UI线程被阻止,有人可以用下面的代码解释发生了什么。感谢

我正在从xamarin.forms viewmodel

异步进行服务调用

以下是流程

查看---> ViewModel --- ClassA ---> ClassB - 从此处拨打服务电话

代码

情景1

public partial class HomePage : ContentPage
{
    private HomeVM model;

    public HomePage()
    {
        InitializeComponent();
        model = new HomeVM();
        model.MainText = ReturnBool().Result;
        this.BindingContext = model;
    }
    public async Task<string> ReturnBool()
    {
        IsBusy = true;
        var r = await new WS().ReturnBool();
        IsBusy = false;---------------------------------------Not hitting the breakpoint here
        return r;
    }
}

public interface IWS
{
    Task<string> ReturnBool();
}

public class WS : IWS
{
    public Task<string> ReturnBool()
    {
        return ServiceOperations.ReturnBool();
    }
}

internal class ServiceOperations
{
    public async static Task<string> ReturnBool()
    {
        var uri = new Uri(string.Format("http://testmyapi.azurewebsites.net/", string.Empty)); 
        try
        {
            HttpClient client = new HttpClient();
            client.BaseAddress = uri;
            HttpResponseMessage response = null;
            response = await client.GetAsync("/api/Values/Get");
            if (response.IsSuccessStatusCode)
            {
                var content = await response.Content.ReadAsStringAsync();
                string str = JsonConvert.DeserializeObject<string>(content);
                return str;
            }
            else {
                return null;
            }
        }
        catch (Exception)
        {
            return null;
        }
    }
}

情景2

public partial class HomePage : ContentPage
{
    private HomeVM model;

    public HomePage()
    {
        InitializeComponent();
        model = new HomeVM(); 
        this.BindingContext = model;
    } 
}

public class HomeVM : BaseVM
{
    private string mainText;

    public string MainText
    {
        get { return mainText; }
        set
        {
            mainText = value;
            RaisePropertyChanged("MainText");
        }
    }

    public HomeVM()
    {
        MainText = ReturnBool().Result; 
    }

    public async Task<string> ReturnBool()
    {
        IsBusy = true;
        var r = await new WS().ReturnBool();
        IsBusy = false;---------------------------------------Not hitting the breakpoint here
        return r;
    }
}


public interface IWS
{
    Task<string> ReturnBool();
}

public class WS : IWS
{
    public Task<string> ReturnBool()
    {
        return ServiceOperations.ReturnBool();
    }
}

internal class ServiceOperations
{
    public async static Task<string> ReturnBool()
    {
        var uri = new Uri(string.Format("http://testmyapi.azurewebsites.net/", string.Empty)); 
        try
        {
            HttpClient client = new HttpClient();
            client.BaseAddress = uri;
            HttpResponseMessage response = null;
            response = await client.GetAsync("/api/Values/Get");
            if (response.IsSuccessStatusCode)
            {
                var content = await response.Content.ReadAsStringAsync();
                string str = JsonConvert.DeserializeObject<string>(content);
                return str;
            }
            else {
                return null;
            }
        }
        catch (Exception)
        {
            return null;
        }
    }
}

1 个答案:

答案 0 :(得分:2)

您正在使用ReturnBool()。结果在构造函数中。返回调用将阻止您的UI线程。将该代码移动到控制器操作方法而不使用&#34; .Result&#34;部分。确保方法是异步的并始终返回任务。