C# 在Xamarin消费REST API中显示列表

C# 在Xamarin消费REST API中显示列表,c#,listview,xamarin,mvvm,xamarin.forms,C#,Listview,Xamarin,Mvvm,Xamarin.forms,我想在Xamarin(VS 2017)中显示使用API的产品列表,但在运行我的应用程序时,它会显示一个空列表(如图所示) 对于以上内容,我通过邮递员确认该服务可供使用 然后,我创建了一个名为ApiServices的服务,它使用API: APISERVICE.CS: public async Task<Response> GetList<T>(string urlBase, string servicePrefix, string controller)

我想在Xamarin(VS 2017)中显示使用API的产品列表,但在运行我的应用程序时,它会显示一个空列表(如图所示)

对于以上内容,我通过邮递员确认该服务可供使用

然后,我创建了一个名为ApiServices的服务,它使用API:

APISERVICE.CS:

public async Task<Response> GetList<T>(string urlBase, string servicePrefix, string controller)
        {
            try
            {
                var client = new HttpClient();          
                client.BaseAddress = new Uri(urlBase);
                var url = string.Format("{0}{1}", servicePrefix, controller);
                var response = await client.GetAsync(url);
                var result = await response.Content.ReadAsStringAsync();

                if (!response.IsSuccessStatusCode)
                {
                    return new Response
                    {
                        IsSuccess = false,
                        Message = result,
                    };
                }

                var list = JsonConvert.DeserializeObject<List<T>>(result);

                return new Response
                {
                    IsSuccess = true,
                    Message = "Ok",
                    Result = list,
                };
            }
            catch (Exception ex)
            {
                return new Response
                {
                    IsSuccess = false,
                    Message = ex.Message,
                };
            }
        }
我验证JSON是否到达:

值得一提的是,我正在使用MVVM模式,我已经在MainViewModel中实例化了ProductsViewModel类,并且我已经查看了代码,但是我找不到错误


有什么帮助吗?请继续关注您的评论

由于绑定到
列表视图的模型是一种复杂类型,您需要为
列表视图定义自定义布局

您可以参考以下代码:

<ListView ItemsSource="{Binding Productos}"> 
<ListView.ItemTemplate>
   <DataTemplate>
      <ViewCell>
              <Label Text="{Binding Description}"
                FontSize="Large" 
                FontAttributes="Bold" 
                HorizontalTextAlignment="Start"
                Margin="20,0,0,0"
                VerticalTextAlignment="Center"
                >
    </ViewCell>
  </DataTemplate>
</ListView.ItemTemplate>
</ListView>

返回新的响应{};您的代码中的响应是什么?它是类还是Httpresponsemessage?
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="Fundamentos.Views.ProductosPage"
             BindingContext="{Binding Main, Source={StaticResource Locator}}"
             Title="Productos">

    <ContentPage.Content>
        <StackLayout
            BindingContext="{Binding Productos}"
            Padding="4">

            <ListView 
                ItemsSource="{Binding Productos}">                    
            </ListView>

        </StackLayout>
    </ContentPage.Content>

</ContentPage>
public class Product
    {
        public int ProductId { get; set; }

        public string Description { get; set; }

        public string Remarks { get; set; }

        public string ImagePath { get; set; }

        public Decimal Price { get; set; }

        public bool IsAvailable { get; set; }

        public DateTime PublishOn { get; set; }

        public string ImageFullPath
        {
            get
            {
                if (string.IsNullOrEmpty(this.ImagePath))
                {
                    return "noproduct";
                }

                return $"https://salesbackend.azurewebsites.net/{this.ImagePath.Substring(1)}";
            }

        }

        public override string ToString()
        {
            return this.Description;
        }
    }
<ListView ItemsSource="{Binding Productos}"> 
<ListView.ItemTemplate>
   <DataTemplate>
      <ViewCell>
              <Label Text="{Binding Description}"
                FontSize="Large" 
                FontAttributes="Bold" 
                HorizontalTextAlignment="Start"
                Margin="20,0,0,0"
                VerticalTextAlignment="Center"
                >
    </ViewCell>
  </DataTemplate>
</ListView.ItemTemplate>
</ListView>