【问题标题】:Xamarin Listview don't show the observable CollectionXamarin Listview 不显示可观察的集合
【发布时间】:2018-12-11 22:06:28
【问题描述】:

我正在使用Xamarin.Forms MVVM 开发我的应用程序,但没有发现我做错了什么,我有一个带有来自 Web API 的值的ObservableCollection,当我设置断点时,所有即使在视图中,当我看到绑定源的值时,值也很好,一切都有值,但这些值没有显示在我的 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();
}

【问题讨论】:

  • 你能显示Land实体类吗?
  • 是的,这个类和所有的都有我想要的值,唯一错误的是列表视图没有显示这些值。公共类土地{公共int名称{获取;放; } 公共 int 货币{ 得到;放; } 公共字符串长度 { 获取;放; } 公共十进制语言{ 得到;放; } }
  • 您需要继承 INotifyPropertyChanged 接口并更改属性以在分配值时通知它。有些类似于私有字符串 cardImage;公共字符串 CardImage { 获取 { 返回 cardImage; } 设置 { car​​dImage = 值; OnPropertyChanged(); } }

标签: xamarin.forms


【解决方案1】:

检查您的BindingContext。我认为你的观点是错误的。

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

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

确保您的页面ConsultaPageBindingContext 设置为您的视图模型DatosMedicosViewModel

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

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

await Navigation.PushAsync(mypage);

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-06-10
    • 2017-06-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-20
    • 1970-01-01
    • 2021-12-15
    相关资源
    最近更新 更多