在MAUI中,要显示列表,可以使用Listview控件,以下列出实际可运行的代码供参考。本代码在.NET 7上运行成功。
实际运行效果如下图:

HtListviewPage.xaml文件:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="HovertreeMaui.HtPages.HtListviewPage"
Title="Hovertree Listview">
<ScrollView>
<VerticalStackLayout>
<Label
Text="My Devices"
VerticalOptions="Center"
HorizontalOptions="Center" FontSize="32" FontAttributes="Bold" />
<ListView x:Name="htDeviceView" >
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Grid Padding="10">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<Image Grid.RowSpan="2"
Source="{Binding ImageUrl}"
Aspect="AspectFill"
HeightRequest="140"
WidthRequest="180" />
<Label Grid.Column="1"
Text="{Binding Name}"
FontAttributes="Bold" />
<Label Grid.Row="1"
Grid.Column="1"
Text="{Binding Company}"
FontAttributes="Italic"
VerticalOptions="End" />
</Grid>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</VerticalStackLayout>
</ScrollView>
</ContentPage>
HtListviewPage.xaml.cs文件
using HovertreeMaui.HtModels;
namespace HovertreeMaui.HtPages;
public partial class HtListviewPage : ContentPage
{
List<HovertreeDevice> HtDevices = new List<HovertreeDevice>
{
new HovertreeDevice("笔记本电脑","Lenovo","16G内存","https://hovertree.com/hvtimg/201512/agagq0or_l.jpg"),
new HovertreeDevice("电风吹","Midea","星夜蓝","https://hovertree.com/hvtimg/201601/thbniyjx_l.png"),
// ...etc.,... 何问起
new HovertreeDevice("移动硬盘","西部数码","1TB","https://hovertree.com/hvtimg/201512/vt1tf0xl_l.jpg"),
new HovertreeDevice("电脑键盘","Lenovo","2.4G无线连接","https://hovertree.com/hvtimg/bjafjd/iofopnro_l.png")
};
public HtListviewPage()
{
InitializeComponent();
this.htDeviceView.ItemsSource= HtDevices;
}
}
HovertreeDevice.cs文件代码:
namespace HovertreeMaui.HtModels
{
internal class HovertreeDevice
{
public HovertreeDevice(
string name,string company,string details,string imageUrl
) {
Name=name;
Company=company;
Details=details;
ImageUrl=imageUrl;
}
public string Name { get; set; }
public string Company { get; set; }
public string Details { get; set; }
public string ImageUrl { get; set; }
}
}
下一篇继续研究