【问题标题】:Xamarin ListView not showing contentXamarin ListView 不显示内容
【发布时间】:2021-10-16 00:49:45
【问题描述】:

我正在尝试使用 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; } 
    }

【问题讨论】:

  • 您正在初始化WellsCollection 两次,并且您正在从构造函数调用异步方法

标签: listview xamarin.forms data-binding


【解决方案1】:

正如 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 的答案进行改进。

【讨论】:

  • 感谢您的帮助!像魅力一样工作!
【解决方案2】:

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

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);
    }
  }
}

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-06-27
    • 1970-01-01
    • 2018-09-14
    • 1970-01-01
    • 1970-01-01
    • 2018-01-21
    • 1970-01-01
    相关资源
    最近更新 更多