简体   繁体   中英

Xamarin.Forms Working with ListView from existing List of objects

I am using Xamarin.Forms and I am trying to create a ListView from c# List of my model, this is what I have tried...I created this view

<ListView x:Name="lst">
             <ListView.ItemTemplate>
                 <DataTemplate>
                     <TextCell Text="{Binding Email}" />
                 </DataTemplate>
             </ListView.ItemTemplate>
         </ListView>

and I tried to set the ItemSource like so:

List<UserClass> users = new List<UserClass>();

            users = webService.getScannedLog();

            BindingContext = this;
            lst.ItemsSource = users;

but when I do this, I get this error:

Object reference not set to an instance of an object

What am I doing wrong?

You have to set the binding for ItemsSource:

<ListView x:Name="lst"
          ItemsSource="{Binding .}">
             <ListView.ItemTemplate>
                 <DataTemplate>
                     <TextCell Text="{Binding Email}" />
                 </DataTemplate>
             </ListView.ItemTemplate>
         </ListView>

And in the code behind:

List<UserClass> users = new List<UserClass>();

            users = webService.getScannedLog();

            BindingContext = users;

Note that ItemsSource binding was set in XAML.

Motivated by the comment of @Jason on my first answer to this post i got curious to know if he was right when he said:

there is no reason you can't set ItemsSource directly in code

and after a bit of research i found out that as he pointed out, the code in my answer was doing fundamentally the same as the code of the OP. But... how did then my answer help OP to solve the problem?

Well, the only way i can get the exception

Object reference not set to an instance of an object

using the code in the original post is if the c# code posted is called before InitializeComponent() is called, that is, if the code behind looks like:

public MainPage()
{

    List<UserClass> users = new List<UserClass>();

    users = webService.getScannedLog();

    BindingContext = this;
    lst.ItemsSource = users;

    InitializeComponent();

}

in which case lst is being called before it is even created by InitializeComponent() . In that case, my answer solved the issue by removing lst reference and setting ItemsSource directly in XAML .

If that is the case, at all, then i would like to say that to solve the issue it is enough to move the setting of ItemsSource after the call to InitializeComponent() , in which case the code would look something like:

public MainPage()
{

    InitializeComponent();    

    List<UserClass> users = new List<UserClass>();

    users = webService.getScannedLog();

    BindingContext = this;
    lst.ItemsSource = users;

}

@user979331, please let me know if this is the case.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM