在运行时在 Xamarin 中使用 ViewCells 填充 ListView
Populate a ListView with ViewCells at runtime in Xamarin
我试图在运行时用自定义 ViewCell 对象填充 ListView,但出于某种原因,Listview 使用 ViewCell 对象的类型字符串名称填充,而不是用实际的 ViewCell “膨胀”。
问:我需要做什么才能让这些 ViewCells“膨胀”而不是仅仅显示它们的“ToString()”输出?
这是非常简单的 MainPage.xaml 文件:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="ViewCellExperiment.MainPage">
<StackLayout>
<ListView x:Name="mainListView"></ListView>
</StackLayout>
</ContentPage>
这是非常简单的 MainPage.xaml.cs 文件:
using System.Collections.Generic;
using Xamarin.Forms;
namespace ViewCellExperiment
{
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
var ListSource = new List<ViewCell>() { new CustomCell(), new CustomCell(), new CustomCell() };
mainListView.ItemsSource = ListSource;
}
}
}
最后,这里是 CustomCell.cs 文件:
using Xamarin.Forms;
namespace ViewCellExperiment
{
public class CustomCell : ViewCell
{
public CustomCell()
{
StackLayout cellWrapper = new StackLayout();
StackLayout horizontalWrapper = new StackLayout();
Label explanatoryText = new Label() { Text = "Explanatory text Label" };
Button leftButton = new Button();
Button centerButton = new Button();
Button rightButton = new Button();
horizontalWrapper.Orientation = StackOrientation.Horizontal;
cellWrapper.Orientation = StackOrientation.Vertical;
leftButton.Text = "Record";
centerButton.Text = "Play";
rightButton.Text = "Upload";
horizontalWrapper.Children.Add(leftButton);
horizontalWrapper.Children.Add(centerButton);
horizontalWrapper.Children.Add(rightButton);
cellWrapper.Children.Add(explanatoryText);
cellWrapper.Children.Add(horizontalWrapper);
View = cellWrapper;
}
}
}
您的自定义单元格应设置为 ListView
的 DataTemplate
,而不是 ItemsSource
。 ListView
是一个 templated 控件 - 您定义一个模板,并将该模板应用于 ItemsSource
的每个项目
我试图在运行时用自定义 ViewCell 对象填充 ListView,但出于某种原因,Listview 使用 ViewCell 对象的类型字符串名称填充,而不是用实际的 ViewCell “膨胀”。
问:我需要做什么才能让这些 ViewCells“膨胀”而不是仅仅显示它们的“ToString()”输出?
这是非常简单的 MainPage.xaml 文件:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="ViewCellExperiment.MainPage">
<StackLayout>
<ListView x:Name="mainListView"></ListView>
</StackLayout>
</ContentPage>
这是非常简单的 MainPage.xaml.cs 文件:
using System.Collections.Generic;
using Xamarin.Forms;
namespace ViewCellExperiment
{
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
var ListSource = new List<ViewCell>() { new CustomCell(), new CustomCell(), new CustomCell() };
mainListView.ItemsSource = ListSource;
}
}
}
最后,这里是 CustomCell.cs 文件:
using Xamarin.Forms;
namespace ViewCellExperiment
{
public class CustomCell : ViewCell
{
public CustomCell()
{
StackLayout cellWrapper = new StackLayout();
StackLayout horizontalWrapper = new StackLayout();
Label explanatoryText = new Label() { Text = "Explanatory text Label" };
Button leftButton = new Button();
Button centerButton = new Button();
Button rightButton = new Button();
horizontalWrapper.Orientation = StackOrientation.Horizontal;
cellWrapper.Orientation = StackOrientation.Vertical;
leftButton.Text = "Record";
centerButton.Text = "Play";
rightButton.Text = "Upload";
horizontalWrapper.Children.Add(leftButton);
horizontalWrapper.Children.Add(centerButton);
horizontalWrapper.Children.Add(rightButton);
cellWrapper.Children.Add(explanatoryText);
cellWrapper.Children.Add(horizontalWrapper);
View = cellWrapper;
}
}
}
您的自定义单元格应设置为 ListView
的 DataTemplate
,而不是 ItemsSource
。 ListView
是一个 templated 控件 - 您定义一个模板,并将该模板应用于 ItemsSource