首页 > 解决方案 > Xamarin Listview 不显示可观察的集合

问题描述

我正在使用Xamarin.FormsMVVM 开发我的应用程序,但没有发现我做错了什么,我有ObservableCollection来自 web API 的值,当我设置断点时,即使在视图中,所有值都很好查看绑定源的值一切都有值,但值没有显示在我的ListView.

这是视图模型

class DatosMedicosViewModel : BaseViewModel
{
    private ApiService apiService;

    private ObservableCollection<Land> land;
    private bool isRefreshing;

    public ObservableCollection<Land> Lands
    {
        get { return this.land; }
        set { SetValue(ref this.land, value); }
    }

    public bool IsRefreshing
    {
        get { return this.isRefreshing; }
        set { SetValue(ref this.isRefreshing, value); }
    }

    public DatosMedicosViewModel()
    {
        this.apiService = new ApiService();
        this.LoadLand();
    }

    private async void LoadLand()
    {
        this.IsRefreshing = true;
        var connection = await this.apiService.CheckConnection();
        if (!connection.IsSuccess)
        {
            this.IsRefreshing = false;
            await Application.Current.MainPage.DisplayAlert(
                "Error",
                connection.Message,
                "Accept");
            await Application.Current.MainPage.Navigation.PopAsync();
            return;
        }

        var response = await this.apiService.GetList<Land>(
           "url Base",
           "prefix",
           "Controller");

        if (!response.IsSuccess)
        {
            this.IsRefreshing = false;
            await Application.Current.MainPage.DisplayAlert(
                "Error",
                response.Message,
                "Accept"
                );
            return;
        }

        var list = (List<Land>)response.Result;
        this.Lands = new ObservableCollection<Land>(list);
        this.IsRefreshing = false;
    }

    public ICommand RefreshCommand
    {
        get
        {
            return new RelayCommand(LoadLand);
        }
    }
}

这是视图

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="ARLAPP.Views.ConsultaPage"
             BackgroundColor="White"
             BindingContext="{Binding Main, Source={StaticResource Locator}}"
             Title="Lands">
    <ContentPage.Content>
        <StackLayout 
            BindingContext="{Binding Lands}"
            Padding="5">
            <StackLayout>
                <Image 
                VerticalOptions="Center"
                WidthRequest="300"
                Source="UserIcon"
                BackgroundColor="Transparent"/>
                <Label Text="Mark"
                       VerticalOptions="Center"
                       HorizontalOptions="CenterAndExpand"
                       FontAttributes="Bold"
                       FontSize="Medium"/>
            </StackLayout>
            <StackLayout>
                <ListView
                SeparatorVisibility="Default"
                FlowDirection="LeftToRight"
                BackgroundColor="White"
                ItemsSource="{Binding Lands}"
                HasUnevenRows="True">
                    <ListView.ItemTemplate>
                        <DataTemplate>
                            <ViewCell>
                                <Label   
                                    Grid.Column="2"
                                    VerticalOptions="Center"
                                    TextColor="Black"
                                    Text="{Binding Currency}"/>

                            </ViewCell>
                        </DataTemplate>
                    </ListView.ItemTemplate>
                </ListView>
            </StackLayout>
        </StackLayout>
    </ContentPage.Content>
</ContentPage>

这里我如何称呼视图

if (this.PageName == "Lands")
{
    MainViewModel.GetInstance().Lands= new LandViewModel();
    Application.Current.MainPage = new LandMasterPage();
}

标签: xamarin.forms

解决方案


检查您的BindingContext. 我认为您认为您的设置是错误的。

在您的顶层中,StackLayout您将 the 设置BindingContext为您的属性:BindingContext="{Binding Lands}"。并在您的ListView设置中ItemsSource也将此属性设置为:ItemsSource="{Binding Lands}". 这不起作用,因为ListView试图绑定到Lands您的 内部的属性BindingContext,该属性也设置为Lands.

BindingContext从您的顶层删除StackLayout,因为您不需要它。

确保BindingContext您的页面ConsultaPage设置为您的 view-model DatosMedicosViewModel

设置绑定上下文的示例(抽象代码):

var mypage = new ConsultaPage();
mypage.BindingContext = new DatosMedicosViewModel();

await Navigation.PushAsync(mypage);

// Load your data in OnAppearing() of the page-event

这应该可以解决您的绑定问题。

旁注:正如 Abdul Gani 在评论中所说:确保您实现INotifyPropertyChanged接口,但我假设您已经在您的 -Method 中执行此操作BaseViewModel并调用 NotifyChanged-Event SetValue


推荐阅读