【问题标题】:Why does my listview appear to be empty?为什么我的列表视图看起来是空的?
【发布时间】:2015-10-02 09:53:33
【问题描述】:

我是 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;
    }

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

【问题讨论】:

    标签: c# xaml xamarin xamarin.forms


    【解决方案1】:

    这真的取决于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>
    

    【讨论】:

      【解决方案2】:

      我不认为 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; }
      }
      

      【讨论】:

        【解决方案3】:

        当你从Xaml绑定一些东西时,它应该在页面的BindingContext里面。这就是调用 MVVMBinding 的东西。

        • 示例模型:

          上课预约 { 公共雇员雇员{get;set;} 公共字符串 Place{get;set;} 公共日期时间时间{get;set;} }

        • 示例视图模型:

          公共类 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
              });
           }
          

          }

        • 示例视图类初始化

          公共部分类 Home : ContentPage { 公共主页 () { 初始化组件(); 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>
        

        【讨论】:

        • 快速澄清一下,您在 XAML 页面中关于绑定上下文的声明是正确的,但是从 ListViewDataTemplate 中,绑定上下文变成了您绑定 ListView 的内容的ItemSource 到。所以ContentPageBindingContext会在ListViewItemSource中使用,但不会在ListViewsDataTemplate中使用。
        【解决方案4】:

        就像提到的@jstreet 一样,它可能与您如何将Label 绑定到ItemSource 中的单个项目有关。 LabelText 属性应绑定到单个Appointment 内的属性。在上面的代码中,您尝试将LabelText 属性绑定到与ListViewItemsSource 中的Appointment 实例无关的变量。

        因此,如果Appointment 有一个Name 属性,并且您想在appointmentList 中显示每个AppointmentName,您可以将ListViewLabel 设置为:

        <ListView ItemSource={Binding appointmentList}>
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell> 
                        <ViewCell.View>
                            <Label TextColor="Black" Text="{Binding Name}"/>
                        </ViewCell.View>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
        

        或者,如果您想将LabelsText 属性绑定到特定AppointmentEmployee 内的属性(例如EmployeeName,假设@987654345 @ 对象有一个名为Name 的属性,而Appointment 对象有一个名为Employee 的属性),您的标签将变为:

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2010-10-26
          • 1970-01-01
          • 2015-07-30
          • 2011-05-28
          • 1970-01-01
          • 2015-04-01
          • 2017-03-04
          相关资源
          最近更新 更多