Newby question about ListView

mattsmall1972mattsmall1972 USMember
edited October 2014 in Cross Platform with Xamarin

I've added a new ContentPage to my project:

<?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="AppName.AppList">
<ListView x:Name="MyListView">
      <ListView.ItemTemplate>
        <DataTemplate>
            <Label Text="{Binding Name}"/>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

In the codebehind, I have this:

public partial class AppList 
{
    public AppList ()
    {
        InitializeComponent ();

        List<Person> People = new List<Person> ();
        People.Add (new Person ("Abigail"));
        People.Add (new Person("Bob"));
        People.Add (new Person("Yvonne"));
        People.Add (new Person("Zachary"));
        MyListView.ItemsSource = People;
    }
}

class Person
{
    public Person(string name)
    {
        this.Name = name;

    }

    public string Name { set; get; }
};

When I run this code, I get the following unhandled exception:

  • _exception {System.InvalidCastException: Specified cast is not valid.} System.Exception {System.InvalidCastException}

Maybe I'm thinking this is just like Windows Phone dev too much...
What's wrong here?

Posts

  • JohnMckay.5741JohnMckay.5741 GBMember

    You ever figure this one out? I have the same problem

  • MiltonCondoriMiltonCondori USMember

    im not sure but maybe you have tu use a viewcell in a data template


    <ViewCell.View>

  • TitoLimTitoLim USMember

    You need to add a <ViewCell> tag under your <DataTemplate> tag:

    <?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="AppName.AppList">
        <ListView x:Name="MyListView">
            <ListView.ItemTemplate>
                <ViewCell>
                    <DataTemplate>
                        <Label Text="{Binding Name}"/>
                    </DataTemplate>
                </ViewCell>
            </ListView.ItemTemplate>
        </ListView>
    </ContentPage>
    
Sign In or Register to comment.