从列表中填充列表视图
Populating listview from a list
在 Xamarin 文档 https://docs.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/boxview 中给出的示例“使用 BoxView 列出颜色”的启发下,我一直在尝试以相同的方式填充我的列表视图。然而什么也没有出现。
在我的 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()
{
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 class如下:
<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>
我已经检查过“全部”列表是否已填充,但我不知道应该怎么做才能显示它。
你上面的代码中有两个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.where 感到困惑,你是否使用此 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>
在 Xamarin 文档 https://docs.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/boxview 中给出的示例“使用 BoxView 列出颜色”的启发下,我一直在尝试以相同的方式填充我的列表视图。然而什么也没有出现。
在我的 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()
{
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 class如下:
<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>
我已经检查过“全部”列表是否已填充,但我不知道应该怎么做才能显示它。
你上面的代码中有两个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.where 感到困惑,你是否使用此 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>