Why does my list look empty?

I am new to Xamarin Forms and XAML and I would like to fill up with <ListView>appointments. My XAML looks like this:

<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>

And the code is as follows:

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

There is nothing on the list when I debug the application, I would really appreciate it if someone could tell me what I'm doing wrong!

+4
source share
4 answers

It depends, as defined Employeeand Appointment. For example:

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);
    }
}

Then your XAML should be:

<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

+2
source

I do not think that the listview control can display complex objects like the ones you are trying to do (but not 100% sure about this). Something like that:

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; }
}
+1

@jstreet, , , , Label ItemSource. Label Text , Appointment. Label Text , Appointment, ListView ItemsSource.

, Appointment Name, Name Appointment 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 Text Appointment Employee (, Employee Name), Employee , Name Appointment , Employee), :

0

- Xaml, BindingContext . Thats call MVVM .

  • :

    {   public Employee Employee {get; set;}   public string Place {get; set;}   public DateTime time {get; set;} }

  • ViewModel:

    public class TestPageVM: INotifyPropertyChanged {Interface # region INotifyPropertyChanged public event 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
        });
     }
    

    }

  • Initializing the sample request class :

    public partial class Home: ContentPage {public Home () {InitializeComponent (); this.BindingContext = new TestPageVM (); }}

-Xaml Usage:

<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>
0
source

Source: https://habr.com/ru/post/1609940/


All Articles