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

.NET Maui中如何访问CollectionView内Entry字段的值

在MAUI中管理CollectionView中多个Entry的值的最优方案

最符合MAUI框架设计的方案是基于MVVM的数据绑定+实体模型,比字典或数组更易维护、更优雅,还能自动实现UI与数据的同步,无需手动处理EntryCompleted事件。

具体实现步骤

1. 创建实体模型类

定义一个实现INotifyPropertyChanged的模型类,用来存储单个Entry的内容(可扩展其他属性,比如标识、验证规则等):

using System.ComponentModel;
using System.Runtime.CompilerServices;

public class EntryItem : INotifyPropertyChanged
{
    // 可选:给每个Entry加唯一标识,方便后续按键取值
    public string Key { get; set; }

    private string _inputText;
    public string InputText
    {
        get => _inputText;
        set
        {
            if (_inputText != value)
            {
                _inputText = value;
                OnPropertyChanged();
            }
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}

2. 在ViewModel中管理集合

使用ObservableCollection<EntryItem>作为CollectionView的数据源,它会自动通知UI更新,同时双向绑定会同步用户输入到模型:

using System.Collections.ObjectModel;

public class YourViewModel
{
    public ObservableCollection<EntryItem> EntryCollection { get; set; }

    public YourViewModel()
    {
        // 初始化示例数据,可根据业务逻辑动态添加/删除项
        EntryCollection = new ObservableCollection<EntryItem>
        {
            new EntryItem { Key = "Username", InputText = "" },
            new EntryItem { Key = "Email", InputText = "" },
            new EntryItem { Key = "Phone", InputText = "" }
        };
    }

    // 访问所有Entry值的方法,直接遍历集合即可
    public void ProcessAllInputs()
    {
        foreach (var item in EntryCollection)
        {
            var key = item.Key;
            var value = item.InputText;
            // 这里处理每个Entry的内容,比如存入数据库、提交接口等
        }
    }

    // 按标识取值的示例
    public string GetInputByKey(string key)
    {
        return EntryCollection.FirstOrDefault(x => x.Key == key)?.InputText;
    }
}

3. XAML中绑定CollectionView

在页面的XAML里,将CollectionView的数据源绑定到ViewModel的集合,同时每个Entry的Text属性双向绑定到模型的InputText:

<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="YourNamespace.YourPage"
             BindingContext="{StaticResource YourViewModel}">
    <CollectionView ItemsSource="{Binding EntryCollection}">
        <CollectionView.ItemTemplate>
            <DataTemplate>
                <VerticalStackLayout Margin="10">
                    <Label Text="{Binding Key}" />
                    <Entry Text="{Binding InputText, Mode=TwoWay}" 
                           Placeholder="请输入内容"/>
                </VerticalStackLayout>
            </DataTemplate>
        </CollectionView.ItemTemplate>
    </CollectionView>
</ContentPage>

为什么这比字典/数组更优

  • 自动同步:双向绑定让用户输入直接更新模型,无需手动监听EntryCompleted事件,减少冗余代码。
  • 可扩展性:模型类可轻松添加属性(比如输入验证规则、提示文本),适配复杂业务场景。
  • 响应式更新:ObservableCollection会自动同步CollectionView的增删项操作,UI自动刷新,无需手动处理集合变更。
  • 符合MVVM设计:分离UI与业务逻辑,代码更清晰,方便单元测试。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.11 20:20:33