简体   繁体   English

Xamarin.Forms 使用现有对象列表中的 ListView

[英]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 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:我尝试像这样设置 ItemSource:

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 Object 引用未设置为 object 的实例

What am I doing wrong?我究竟做错了什么?

You have to set the binding for ItemsSource:您必须为 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.请注意,ItemsSource 绑定是在 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:受到@Jason 在我对这篇文章的第一个回答的评论的启发,我很想知道他说的是否正确:

there is no reason you can't set ItemsSource directly in code没有理由不能直接在代码中设置 ItemsSource

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.经过一番研究,我发现正如他所指出的那样,我的答案中的代码与 OP 的代码基本相同。 But... how did then my answer help OP to solve the problem?但是......那么我的回答如何帮助 OP 解决问题?

Well, the only way i can get the exception好吧,我能得到例外的唯一方法

Object reference not set to an instance of an object Object 引用未设置为 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:使用原始帖子中的代码是如果在调用InitializeComponent()之前调用了发布的 c# 代码,也就是说,如果后面的代码如下所示:

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() .在这种情况下, lst甚至在InitializeComponent()创建之前就被调用了。 In that case, my answer solved the issue by removing lst reference and setting ItemsSource directly in XAML .在这种情况下,我的回答通过删除lst引用并直接在XAML中设置ItemsSource解决了这个问题。

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:如果是这样的话,那么我想说,要解决这个问题,在调用InitializeComponent()之后移动ItemsSource的设置就足够了,在这种情况下,代码将类似于:

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. @user979331,如果是这种情况,请告诉我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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