首页 > 解决方案 > 从列表中填充列表视图

问题描述

在 Xamarin 文档https://docs.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/boxview中给出的示例“使用 BoxView 列出颜色”的启发下,我一直在尝试填充我的列表视图以同样的方式。然而什么都不会出现。

在我创建对象的类 PostList 中,它看起来像这样:

public String Body { get; set; }
public String Subject { get; set; }
public Coordinate Position { get; set; }

private List<Post> all = new List<Post>();

public PostList(List<Post> list)
{
       
        all = list;
        method();
}

public void method()
{
        List<PostList> All = new List<PostList>();
       
       foreach(Post x in all)
       {
          PostList Pl = new PostList
          {
                
                Subject = x.getSubject(),
                Body = x.getBody(),
                Position = x.getCoordinate()
          };
          All.Add(Pl);
      }
}
public static IList<PostList> All { set; get; }

下面给出了用于显示的 XAML 类:

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         xmlns:local="clr-namespace:EE.Model"
         x:Class="EE.View.CWT"
         NavigationPage.HasNavigationBar="False"
         NavigationPage.BackButtonTitle="False"
         ControlTemplate="{StaticResource Template}">

    <ListView SeparatorVisibility="Default"
          ItemsSource="{x:Static local:PostList.All}">
    

    <ListView.RowHeight>
        <OnPlatform x:TypeArguments="x:Int32">
            <On Platform="iOS, Android" Value="180" />
        </OnPlatform>
    </ListView.RowHeight>
    <ListView.WidthRequest Value="20"/>

    <ListView.ItemTemplate>
        <DataTemplate>
            <ViewCell>
                <ContentView Margin="5,5,5,5">
                    <Frame OutlineColor="LightGray">
                        <StackLayout>
                            <StackLayout Orientation="Horizontal">
                                <StackLayout x:Name="SL" Padding="0">
                                    <Label x:Name="Title" Text="{Binding Subject}"/>
                                </StackLayout>
                            </StackLayout>
                         </StackLayout>
                    </Frame>
                </ContentView>
            </ViewCell>
        </DataTemplate>
    </ListView.ItemTemplate>

我检查了列表“全部”是否已填充,但我不知道应该做什么才能显示。

标签: xamarinxamarin.formsdata-bindingitemsource

解决方案


上面的代码中有两个All

一个是

public static IList<PostList> All { set; get; },

另一个在你的method()

List<PostList> All = new List<PostList>();

尝试更改它以使它们保持一致:

public void method()
{
   All = new List<PostList>();
   
   foreach(Post x in all)
   {
      PostList Pl = new PostList
      {
            
            Subject = x.getSubject(),
            Body = x.getBody(),
            Position = x.getCoordinate()
      };
      All.Add(Pl);
  }
}

我也对你如何实例化你的 ViewModel 感到困惑。你在哪里用这个public PostList(List<Post> list) 构造函数传递参数?

我们通常这样做:

<ListView SeparatorVisibility="Default"
      x:Name ="listview">
    ...

</ListView>

在后面的代码中:

public YourPage()
  {
      InitializeComponent();
      PostList list = new PostList(your List<Postlist>);
      listview.ItemsSource = list.All;
  }

视图模型:

public class PostList
{

  public String Body { get; set; }
  public String Subject { get; set; }
  public Coordinate Position { get; set; }
  private List<Post> all = new List<Post>();
  public PostList(List<Post> list)
  {
   
    all = list;
    method();
  }

  public void method()
  {
    All = new List<PostList>();
   
    foreach(Post x in all)
    {
      PostList Pl = new PostList
      {
            
            Subject = x.getSubject(),
            Body = x.getBody(),
            Position = x.getCoordinate()
      };
      All.Add(Pl);
    }  
  }
  public IList<PostList> All { set; get; }
}

或使用x:Static资源:

public class PostList
{

  public String Body { get; set; }
  public String Subject { get; set; }
  public Coordinate Position { get; set; }
  private static List<Post> all = new List<Post>();
  static PostList()
  {

    all = xxx;//here create a List<Post>
    method();
  }

  public static void method()
  {
    All = new List<PostList>();

    foreach(Post x in all)
    {
      PostList Pl = new PostList
      {

        Subject = x.getSubject(),
        Body = x.getBody(),
        Position = x.getCoordinate()
      };
      All.Add(Pl);
    }  
   }
  public IList<PostList> All { set; get; }
}

然后你可以在你的 xaml 中绑定,比如:

<ListView SeparatorVisibility="Default"
      ItemsSource="{x:Static local:PostList.All}">
  ...
</ListView>

推荐阅读