wpf Xamarin 表单列表视图数据绑定

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28628099/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 12:58:07  来源:igfitidea点击:

Xamarin Forms listview databinding

c#wpfxamllistviewxamarin

提问by Guido Magrin

I am currently following this guide:

我目前正在遵循本指南:

http://www.morganskinner.com/2015/01/xamarin-forms-contacts-search.html

http://www.morganskinner.com/2015/01/xamarin-forms-contacts-search.html

I am willing to make a contacts page in my app, like the one showed in the pictures there, still something is not working out as it should for me:

我愿意在我的应用程序中创建一个联系人页面,就像那里的图片中显示的那样,但仍有一些事情没有像我应该的那样工作:

I wrote/copied the XAML:

我编写/复制了 XAML:

    <Grid.RowDefinitions>
      <RowDefinition Height="Auto"/>
      <RowDefinition Height="*"/>
    </Grid.RowDefinitions>

    <SearchBar x:Name="search" Placeholder="Search"/>

    <ListView x:Name="ProvaView" Grid.Row="1" ItemsSource="{Binding FilteredContacts}" IsGroupingEnabled="true" GroupDisplayBinding="{Binding Key}" GroupShortNameBinding="{Binding Key}">
      <ListView.ItemTemplate>
        <DataTemplate>
          <TextCell Text="{Binding Name}" TextColor="Black" Detail="{Binding PhoneNumber}" DetailColor="Gray">
          </TextCell>
        </DataTemplate>
      </ListView.ItemTemplate>
    </ListView>

  </Grid>

and now I am trying to "display something", not dinamically loaded (yet), from a list of data.

现在我试图从数据列表中“显示一些东西”,而不是动态加载(还)。

I declared a class like this:

我声明了一个这样的类:

 public class Contact
    {
        public string Name { get; set; }
        public string PhoneNumber { get; set; }
    }
}

and now the code behind the xaml is this:

现在 xaml 背后的代码是这样的:

public partial class ContactsPage : MasterDetailPage
    {
        public List<Contact> FilteredContacts = new List<Contact>
            {
                new Contact
                {
                    Name = "Guido",
                    PhoneNumber = "3292773477"
                },
                new Contact
                {
                    Name = "Luca",
                    PhoneNumber = "3472737445"
                },
                new Contact
                {
                    Name = "Luca",
                    PhoneNumber = "3472737445"
                }
            };

        public ContactsPage()
        {
            InitializeComponent();
            ProvaView.ItemsSource = FilteredContacts;
        }
}

When I launch the app, all I get anyway is an empty listview, with 3 fields but nothing in it.

当我启动应用程序时,我得到的只是一个空的列表视图,有 3 个字段,但没有任何内容。

Where am I doing wrong?

我哪里做错了?

Also, straight from the page where I am working off, I can't understand the meaning of these Fields associated to the listview:

此外,直接从我正在处理的页面中,我无法理解与列表视图相关的这些字段的含义:

GroupDisplayBinding="{Binding Key}" GroupShortNameBinding="{Binding Key}"

GroupDisplayBinding="{Binding Key}" GroupShortNameBinding="{Binding Key}"

and as much as I could look around on the web, I coudln't find any reference to both of them.

尽我所能在网上环顾四周,我找不到任何关于他们两个的参考。

回答by Toni Petrina

Most likely the problem is with the way you declared FilteredContacts, it should be a property, and not a field. The reflection mechanism used in binding only searches for properties, fields are not included.

问题很可能出在您声明的方式上FilteredContacts,它应该是一个属性,而不是一个字段。绑定中使用的反射机制仅搜索属性,不包括字段。