Xamarin ListView 不显示内容

Xamarin ListView not showing content

我正在尝试使用 Xamarin 表单创建一个应用程序,其中一个页面是 ListView。我需要将它绑定到一个 ObservableCollection,它从 Azure SQL 获取它的值,但绑定失败。 Observable 集合确实是从 Web API 获取的,所以我不确定问题出在哪里。感谢您的帮助!

xaml 页数:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" 
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:viewModel="clr-namespace:SiteVisits.ViewModels" xmlns:model="clr-namespace:SiteVisits.Models" 
             x:DataType="viewModel:AllWellsViewModel"
             x:Class="SiteVisits.Views.AllWells">


    <ListView x:Name="WellsCollection"
                ItemsSource="{Binding WellsCollection}"
                HasUnevenRows="True">
        <ListView.ItemTemplate>
            <DataTemplate>
                <ViewCell>
                    <StackLayout Padding="10" x:DataType="model:Well">
                         <Label Text="{Binding Description}" 
                                        FontSize="Large"
                                        VerticalOptions="Center"/>
                    </StackLayout>
                </ViewCell>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
</ContentPage>

xaml.cs :

  public partial class AllWells : ContentPage
    {
        AllWellsViewModel viewModel;

        public AllWells(AllWellsViewModel viewModel)
        {
            InitializeComponent();
            this.viewModel = viewModel;
            BindingContext = this.viewModel;
        }

        public AllWells()
        {
            InitializeComponent();

            viewModel = new AllWellsViewModel();
            BindingContext = viewModel;
        }
    }

视图模型:

  public class AllWellsViewModel : BaseViewModel 
    {
        public ObservableCollection<Well> WellsCollection { get; set; }

        public AllWellsViewModel() 
        {
            Title = "Browse";
            WellsCollection = new ObservableCollection<Well>();
            InitializeData();

        }
        async void InitializeData()
        {
            var wellDataStore = new WellDataStore();

            var wells = await wellDataStore.GetAllWells();
            if (wells != null)
            {
                WellsCollection = new ObservableCollection<Well>(wells);
            }
        }
    }

型号:

    public class Well
    {
        [PrimaryKey, AutoIncrement]
        public int Id { get; set; } 
        public string Description { get; set; } 
    }

如果您使用的是 ObservableCollection,您应该只创建一次新的,否则对您绑定它的任何内容的引用也会丢失,您需要重新设置它。只需初始化一次集合,然后每次 clear/fill 它。

async void InitializeData()
{
  var wellDataStore = new WellDataStore();

  var wells = await wellDataStore.GetAllWells();
  if (wells != null)
  {
    WellsCollection.Clear();

    foreach (var well in wells)
    {
      WellsCollection.Add(well);
    }
  }
}

您页面的双重构造函数看起来也有点滑稽。我认为你应该只保留将视图模型作为参数的那个。

正如 Jason 评论的那样,问题是您没有等待 Azure 的结果。

所以从页面的 OnAppearing 而不是 vm 的构造函数调用 InitializeData 方法。

AllWellsViewModel.cs

    public class AllWellsViewModel : BaseViewModel 
    {
        public AllWellsViewModel() 
        {
            Title = "Browse";
            WellsCollection = new ObservableCollection<Well>();
            //remove the method call here
        }
    }

AllWells.xaml.cs

public partial class AllWells : ContentPage
{
    protected override async void OnAppearing()
    {
        //call the method here instead and "AWAIT"
        await vm.InitializeData();
    }
}

此外,还应用 Gerald 的答案进行改进。