Xamarin Listview不显示可观察的集合

时间:2018-12-11 13:09:45

标签: xamarin.forms

我正在使用Xamarin.Forms MVVM开发我的应用程序,但没有发现我做错了什么,我有一个ObservableCollection与Web API中的值,当我设置了断点,即使在视图中看到绑定源的值时,所有值都很好,但所有值都具有值,但是这些值未显示在我的ListView中。

这是ViewModel

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

1 个答案:

答案 0 :(得分:1)

检查您的BindingContext。我认为您认为这是错误的。

在顶级StackLayout中,将BindingContext设置为属性BindingContext="{Binding Lands}"。然后,在ListView中还将ItemsSource设置为此属性:ItemsSource="{Binding Lands}"。这不会起作用,因为ListView试图绑定到Lands内部的属性BindingContext,该属性也设置为Lands

从顶级BindingContext中删除StackLayout,因为您不需要它。

确保将页面BindingContext的{​​{1}}设置为视图模型ConsultaPage

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

DatosMedicosViewModel

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

旁注:正如Abdul Gani在评论中所述:确保您实现了var mypage = new ConsultaPage(); mypage.BindingContext = new DatosMedicosViewModel(); await Navigation.PushAsync(mypage); // Load your data in OnAppearing() of the page-event 接口,但是我想您已经在INotifyPropertyChanged中执行了此操作,并在您的{{中调用了NotifyChanged-Event 1}}-方法。