ListView数据绑定与自定义视图

时间:2016-11-26 04:24:23

标签: c# xaml listview xamarin data-binding

我正在尝试使用xamarin项目中的数据绑定来获取自定义视图的自定义列表(我最终将为每个视图添加控件)。但是,当我运行我的项目时,列表中没有任何内容。我对数据绑定的东西很新,所以我有点困惑。我搜索并尝试了其他一些解决方案,但未能弄清楚我的具体问题。任何帮助,将不胜感激。这是我的代码:

<?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="XamarinTuts.XMainPage">

  <ContentPage.Content>
    <StackLayout>
      <Label Text="Tasks:" HorizontalTextAlignment="Start" VerticalTextAlignment="Start" x:Name="tasksLabel"></Label>
      <ListView ItemsSource="{Binding taskItems}">
        <ListView.ItemTemplate>
          <DataTemplate>
            <Frame BackgroundColor="Black" Padding="5">
              <Grid>
                <StackLayout Name="TextStack" Orientation="Vertical" Grid.Column="0">
                  <Label Text="{Binding Name}"></Label>
                  <Label Text="{Binding Description}"></Label>
                </StackLayout>
                <StackLayout Name="ButtonStack" Orientation="Horizontal" HorizontalOptions="End" Grid.Column="1">
                  <Button Name="Edit" Text="Edit" Width="50" Height="50"></Button>
                </StackLayout>
              </Grid>
            </Frame>
          </DataTemplate>
        </ListView.ItemTemplate>
      </ListView>

    </StackLayout>

  </ContentPage.Content>  

</ContentPage>

那是我的XAML。这是我的XMainPage.cs:

public partial class XMainPage : ContentPage
{


    TestViewModel vm;

    public XMainPage()
    {

        InitializeComponent();

        vm = new TestViewModel();

        TaskListItem testItem = new TaskListItem(this, "Go shopping", "Drive to meijer and get the following: Milk, Eggs, Booze");
        TaskListItem testItem2 = new TaskListItem(this, "Play Overwatch", "Pwn some n00bs");
        vm.taskItems.Add(testItem);
        vm.taskItems.Add(testItem2);


        BindingContext = vm;
    }

}

接下来是我的TestViewModel.cs:

public class TestViewModel : INotifyPropertyChanged
{

    ObservableCollection<TaskListItem> mtaskItems;

    public TestViewModel()
    {
        mtaskItems = new ObservableCollection<TaskListItem>();
    }


    public ObservableCollection<TaskListItem> taskItems
    {
        get
        {
            return mtaskItems;
        }
        set
        {
            mtaskItems = value;
        }
    }
    public event PropertyChangedEventHandler PropertyChanged;

    void OnPropertyChanged(string name = null)
    {

        var changed = PropertyChanged;

        if (changed == null)
            return;

        changed.Invoke(this, new PropertyChangedEventArgs(name));

    }
}

后面是我的TaskListItem.cs:

public class TaskListItem : ViewCell
{

    public Task newTask;

    String mName;
    String mDescription;

    public String Name
    {
        get
        {
            return mName;
        }
    }

    public String Description
    {
        get
        {
            return mDescription;
        }
    }


    public TaskListItem(ContentPage taskListItemParent, String TaskName, String TaskDescription)
    {
        //newTask = new Task(TaskName, TaskDescription);

        mName = TaskName;
        mDescription = TaskDescription;

    }
}

1 个答案:

答案 0 :(得分:0)

您尚未在视图上设置BindingContext属性。您应该创建一个ViewModel并将Observable Collection作为公共属性。

    TestViewModel vm;

    public XMainPage()
    {
        InitializeComponent();

        vm = new TestViewModel();

        BindingContext = vm;
    }

使用Xamarin.Forms时,建议遵循MVVM模式。 Here is a simple sample项目写道,您可以帮助您了解更好的数据绑定以及所有部分如何连接在一起。另外,在this chapter of the Xamarin.Forms book DataBinding中解释得非常好