为什么我的列表视图似乎是空的?

时间:2015-10-02 09:53:34

标签: c# xaml xamarin xamarin.forms

我是Xamarin Forms和XAML的新手,我想填写<ListView>约会。我的XAML看起来像这样:

<ListView x:Name="appointmentsView"
          ItemsSource="{Binding appointmentList}" 
          Grid.Row="1"
          AbsoluteLayout.LayoutBounds="0.50, 0.2, 1, 0.5"
          AbsoluteLayout.LayoutFlags="All">
    <ListView.ItemTemplate>
        <DataTemplate>
            <ViewCell> 
                <ViewCell.View>
                    <Label TextColor="Black" Text="{Binding app1}"></Label>
                </ViewCell.View>
            </ViewCell>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

代码如下:

public partial class Home : ContentPage
{
    public ObservableCollection<Appointment> appointmentList = new ObservableCollection<Appointment> ();

    public Employee Tom { get; set; }
    public Appointment app1 { get; set; }

    public Home ()
    {
        InitializeComponent ();

        Tom = new Employee("Tom", "Tommen");
        app1 = new Appointment(new DateTime(), Tom);

        appointmentList.Add(app1);
        appointmentsView.ItemsSource = appointmentList;
    }

当我调试应用程序时,列表中没有任何内容,如果有人能告诉我我做错了什么,我真的很感激!

4 个答案:

答案 0 :(得分:2)

这实际上取决于EmployeeAppointment的定义方式。例如:

public class Appointment
{
    public DateTime AppointmentDateTime { get; set; }
    public Employee AppointmentEmployee { get; set; }

    public Appointment(DateTime AppointmentDateTime, Employee AppointmentEmployee)
    {
        this.AppointmentDateTime = AppointmentDateTime;
        this.AppointmentEmployee = AppointmentEmployee;
    }
}

public class Employee
{
    public string FirstName { get; set; }
    public string LastName { get; set; }

    public Employee(string FirstName, string LastName)
    {
        this.FirstName = FirstName;
        this.LastName = LastName;
    }

    override public string ToString()
    {
        return string.Format("{0}, {1}", LastName, FirstName);
    }
}

然后你的XAML应该是:

<ListView x:Name="appointmentsView"
      ItemsSource="{Binding appointmentList}" 
      Grid.Row="1">
        <ListView.ItemTemplate>
        <DataTemplate>
                <Label Text="{Binding AppointmentEmployee}"></Label>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>

enter image description here

答案 1 :(得分:1)

我不认为listview控件可以像你想要的那样显示复杂的对象(虽然不是100%肯定)。像这样:

public partial class Home : ContentPage
{
    public ObservableCollection<Appointment> appointmentList;

    public Home ()
    {
        InitializeComponent ();

        appointmentList = new ObservableCollection<Appointment>()
        {
            new Appointment { Name = "Tom Tommen", AppointmentDate = DateTime.Now }
        };

        appointmentsView.ItemsSource = appointmentList;
    }
}

public class Appointment
{
    public string Name { get; set; }
    public DateTime AppointmentDate { get; set; }
}

答案 2 :(得分:0)

就像提到的@jstreet一样,它可能与您Label绑定到ItemSource中单个项目的方式有关。 Label的{​​{1}}属性应绑定到单个Text内的属性。在上面的代码中,您尝试将Appointment的{​​{1}}属性绑定到与{{1}内的Label实例无关的变量} {&#39; s Text

因此,如果Appointment具有ListView属性,并且您希望在ItemsSource中显示每个Appointment的{​​{1}},那么您就可以{ {1}}和Name进入:

Name

或者,如果您想将Appointment的{​​{1}}属性绑定到特定appointmentList ListView内的属性(例如假设Label对象具有名为<ListView ItemSource={Binding appointmentList}> <ListView.ItemTemplate> <DataTemplate> <ViewCell> <ViewCell.View> <Label TextColor="Black" Text="{Binding Name}"/> </ViewCell.View> </ViewCell> </DataTemplate> </ListView.ItemTemplate> </ListView> 的属性且Labels对象具有名为{{1}的属性,Text&#39; s Appointment }),你的标签将成为:

答案 3 :(得分:0)

Xaml绑定某些内容时,它应位于页面的BindingContext内。多数人称之为 MVVM Binding 的事情可以解决这个问题。

  • 样本型号:

    课程预约 {     公共员工员工{get; set;}     public string Place {get; set;}     public DateTime time {get; set;} }

  • 示例ViewModel:

    公共类TestPageVM:INotifyPropertyChanged {     #region INotifyPropertyChanged界面     公共事件PropertyChangedEventHandler PropertyChanged;

    protected void OnPropertyChanged(string propertyName)
    {
        var handler = PropertyChanged;
        if (handler != null)
            handler(this, new PropertyChangedEventArgs(propertyName));
    }
    
    #endregion
    
    private ObservableCollection<Appointment> appointmentList;
    public ObservableCollection<Appointment> AppoitmentList
     {
        get{return appointmentList;}
        set
        {
            appointmentList = value;
            OnPropertyChanged("AppoitmentList");
        }
     }
     //ctor
    
     public TestPageVM()
     {
        appointmentList = new ObservableCollection<Appointment>();
        AppoitmentList.Add(new Appointment(){
            Employee = new Employee("Johm Doe"),
            Place = "Houston",
            DateTime = DateTime.Now
        });
     }
    

    }

  • 示例视图类初始化

    public partial class主页:ContentPage {     公共之家()     {         InitializeComponent();         this.BindingContext = new TestPageVM();     } }

-Xaml用法:

<ListView x:Name="appointmentsView"
          ItemsSource="{Binding AppoitmentList}" 
          Grid.Row="1"
          AbsoluteLayout.LayoutBounds="0.50, 0.2, 1, 0.5"
          AbsoluteLayout.LayoutFlags="All">
    <ListView.ItemTemplate>
        <DataTemplate>
            <ViewCell> 
                <ViewCell.View>
                    <Label TextColor="Black" Text="{Binding Employee.Name}"/>
                    <!--can also show place of appointment like: <Label TextColor="Black" Text="{Binding Place}"/> -->
            </ViewCell>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>