You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

.NET Maui技术问询:如何将未知大小的列表/数组绑定到XAML?

问题描述

尝试仅通过XAML代码将LTEInfo类中未知大小的列表(或数组,选简便的)绑定到界面,因大小未知无法逐个设置Label。当前Bands属性已有整数数据,但界面未显示对应内容,相关代码如下:

XAML代码

<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:CellularSignal1"
             x:Class="CellularSignal1.SignalInfo"
             x:DataType="local:SignalInfo"
             Title="Signal">
    <CollectionView ItemsSource="{Binding lteInfos}">
        <CollectionView.ItemTemplate>
            <DataTemplate x:DataType="local:LTEInfo">
                <Grid Margin="0,10">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition Height="Auto"/>
                    </Grid.RowDefinitions>

                    <Label Grid.Row="0" Text="{Binding Pci,StringFormat='PCI: {0:F0}'}"/>
                    <ListView Margin="20">
                        <ListView.ItemTemplate>
                            <DataTemplate>
                                <StackLayout Orientation="Horizontal">
                                    <Label Text="{Binding Bands}" />
                                </StackLayout>
                            </DataTemplate>
                        </ListView.ItemTemplate>
                    </ListView>
                </Grid>

            </DataTemplate>
        </CollectionView.ItemTemplate>
    </CollectionView>
</ContentPage>

LTEInfo类代码

namespace CellularSignal1
{
    public class LTEInfo : ConnectionCellInfo, IEquatable<LTEInfo>
    {
        public LTEInfo() : base()
        {
        }

        public int Rssi { get; set; }
        public int Rsrp { get; set; }
        public int Rsrq { get; set; }
        public int Level { get; set; }
        public List<int> Bands { get; set; }
       
   }
}

主程序代码

namespace CellularSignal1
{
  public ObservableCollection<LTEInfo> lteInfos { get; set; } = new ObservableCollection<LTEInfo>();
  public class implementation1
  {
    LTEInfo lte = new() 
    {
          Pci = pci,
          Level = strength.Level,
          Rssi = strength.Rssi,
          Rsrp = strength.Rsrp,
          Rsrq = strength.Rsrq,
          Bands = new List<int>() {3, 4, 5}
     };
     lteInfos.add(lte);
  }
}

解决方案

核心问题是嵌套的ListView未绑定数据源,且绑定路径错误:原ListView没有指定要展示的Bands集合,直接绑定Bands会显示集合对象类型而非具体元素。

修改步骤

  1. 用MAUI推荐的CollectionView替代ListView(更轻量、性能更好),并为其设置ItemsSource="{Binding Bands}"
  2. 调整Label的绑定路径为当前集合项(因为Bands是int列表,用{Binding .}即可获取单个数值)

修改后的XAML核心片段:

<Label Grid.Row="0" Text="{Binding Pci,StringFormat='PCI: {0:F0}'}"/>
<!-- 替换原ListView为以下代码 -->
<CollectionView Margin="20" ItemsSource="{Binding Bands}">
    <CollectionView.ItemTemplate>
        <DataTemplate>
            <StackLayout Orientation="Horizontal">
                <Label Text="{Binding .}" />
            </StackLayout>
        </DataTemplate>
    </CollectionView.ItemTemplate>
</CollectionView>

额外优化建议

  • 若Bands需要动态增删元素,将List<int>改为ObservableCollection<int>,集合变化时界面会自动刷新
  • 主程序中lteInfos.add(lte)需改为lteInfos.Add(lte)(C#方法名首字母大写),否则编译报错
  • 确保SignalInfo页面的BindingContext已正确设置为包含lteInfos的实例

内容的提问来源于stack exchange,提问作者rickster26ter1

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.28 05:04:59