【问题标题】:ListView ItemsSource binding not displaying itemsListView ItemsSource 绑定不显示项目
【发布时间】:2016-10-12 22:50:23
【问题描述】:

我正在尝试在 Xamarin 中创建绑定的 ListView。这是 C# 代码:

public partial class LearnPage : ContentPage
{
    public class TodoItem
    {
        public string DisplayName { get; set; }
    }

    //ObservableCollection<TodoItem> Items = new ObservableCollection<TodoItem>();
    private IEnumerable<TodoItem> _items;
    public IEnumerable<TodoItem> Items
    {
        get { return _items; }
        set
        {
            if (Equals(_items, value))
                return;
            _items = value;
            OnPropertyChanged();
        }
    }

    public LearnPage ()
    {
        InitializeComponent();
        BindingContext = this;
        Items = new TodoItem[]{
            new TodoItem{ DisplayName = "Milk cartons are recyclable" }
        };
        //Items.Add(new TodoItem { DisplayName = "Milk cartons are recyclable" });
    }
}

您还可以看到一些带有ObervableCollection 的注释掉的代码,我也尝试过。

这是 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" x:Class="learn.LearnPage">
<ContentPage.Content>
    <StackLayout VerticalOptions="FillAndExpand" Padding="0,10,0,10">
        <ListView ItemsSource="{Binding Items}" RowHeight="40" x:Name="sarasas">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <TextCell Text="{Binding DisplayName}" />
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </StackLayout>
</ContentPage.Content>

当我构建应用程序时,会显示一个空列表。我对 Xamarin 真的很陌生,我想我只是错过了一些明显的东西。任何帮助表示赞赏!

【问题讨论】:

  • 这发生在我身上,我尝试分配给列表 n 不同的方式试试这个: var list = new TodoItem[]{ new TodoItem{ DisplayName = "Milk carton are recyclable" } };项目=列表;看看有没有帮助
  • 感谢您的提示,但问题仍然存在。

标签: c# listview xamarin xamarin.forms


【解决方案1】:

我不确定 ContentPage 是否将 [CallerMemberName] 用于 OnPropertyChanged() 方法。所以我首先会尝试写OnPropertyChanged("Items")

无论哪种方式,如果我是你,我会将关注点分开并将项目移动到 ViewModel 类中,该类实现 INotifyPropertyChanged 本身。如果您想在以后进行测试、添加更多代码(例如命令、注入依赖项等),这将有助于您将 ViewModel 代码与 View 代码分开。

所以你可以开始:

public abstract class BaseViewModel : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    protected void OnPropertyChanged ([CallerMemberName]string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs (propertyName));
    }
}

然后创建您的 ViewModel:

public class LearnViewModel : BaseViewModel
{
    public ObservableCollection<TodoItem> Items { get; } = new ObservableCollection<TodoItem>();

    private ICommand _addTodoItem;
    public ICommand AddTodoItem => 
        _addTodoItem = _addTodoItem ?? new Command(DoAddTodoItem);

    private int _count;
    private void DoAddTodoItem()
    {
        var item = new TodoItem { DisplayName = $"Item {++_count}" };
        // NotifyCollectionChanged must happen on UI thread.
        Device.BeginInvokeOnMainThread (() => {
             Items.Add(item);
        });
    }
}

然后你可以保持你的视图代码像这样:

public partial class LearnPage : ContentPage
{
    public LearnPage ()
    {
        InitializeComponent();
        BindingContext = new LearnViewModel();
    }
}

通常你会调用一个命令来填充你的 ViewModel 中的Items,这可能是通过从 Internet 获取数据,或从数据库加载本地数据等。

在此示例中,您只需将构造函数添加到 LearnViewModel 并调用 DoAddTodoItem 几次:

public class LearnViewModel : BaseViewModel
{
    public LearnViewModel()
    {
        DoAddTodoItem();
        DoAddTodoItem();
    }
    ...

当您启动应用程序时,这应该会显示如下内容:

【讨论】:

猜你喜欢
  • 2011-01-14
  • 2012-01-08
  • 2019-12-27
  • 2017-10-26
  • 2018-06-10
  • 1970-01-01
  • 1970-01-01
  • 2021-05-13
相关资源
最近更新 更多