.NET MAUI CollectionView 无法显示对象列表中的字符串值

问题描述 投票:0回答:1

我有一个模型类

Person
,它有一些字符串属性。在 UI 中,有一些
Entry
字段,当按下按钮时,它们会在 ViewModel 类中执行以下操作:

  • 使用填充对象属性的字段中的值创建一个

    Person
    对象

  • Person
    对象添加到
    ObservableList<Person>
    ,该对象本身是在 ViewModel 类的构造函数中创建的

然后,我尝试在

Person
列表中显示
CollectionView
对象的一些属性。字符串值的列表。我希望它看起来像这样:

我知道

Person
对象正在被创建,并且
ObservableList<Person>
列表正在被填充,因为我在单步执行代码时可以看到这些值。

Person.cs

namespace BindingDemo.Model
{
    public partial class Person : ObservableObject
    {
        #region PersonObjectProperties
        [ObservableProperty]
        private string firstName;
        [ObservableProperty]
        private string lastName;
        [ObservableProperty]
        private string email;
        [ObservableProperty]
        private string password;
        #endregion PersonObjectProperties



        public void ClearFields()
        {
            FirstName = string.Empty;
            LastName = string.Empty;
            Email = string.Empty;
            Password = string.Empty;
        }
    }
}

ViewModel 类:

using BindingDemo.Model;

namespace BindingDemo.ViewModel
{
    public partial class MainPageViewModel : ObservableObject
    {
        public Person Person { get; } = new Person();

        [ObservableProperty]
        private ObservableCollection<Person> persons;
        
        public MainPageViewModel()
        {
            persons = new ObservableCollection<Person>();
        }

        [RelayCommand]
        private void AddPersonToListAndDb()
        {
            // every field must have a value
            if (string.IsNullOrWhiteSpace(Person.FirstName) || 
                string.IsNullOrWhiteSpace(Person.LastName) || 
                string.IsNullOrWhiteSpace(Person.Email) || 
                string.IsNullOrWhiteSpace(Person.Password))
                return;

            // add the Person object to the list of Person objects
            Persons.Add(Person);

            // clear the fields in the UI so the user can add another person
            Person.ClearFields();
           
            // add the Person object to a database
        }
    }
}

查看课程:

<?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="BindingDemo.MainPage"
             Title="Create Person"
             xmlns:viewmodel="clr-namespace:BindingDemo.ViewModel"
             x:DataType="viewmodel:MainPageViewModel">

      <Grid RowDefinitions="Auto, Auto, Auto, Auto, Auto,*"
            Margin="10">
            <Entry Grid.Row ="0" 
                   Placeholder="Enter your first name"
                   PlaceholderColor="DarkGoldenrod"
                   FontAttributes="Bold"
                   Text="{Binding Person.FirstName}" />

            <Entry Grid.Row="1"
                   Placeholder="Enter your last name"
                   PlaceholderColor="DarkGoldenrod"
                   FontAttributes="Bold"
                   Text="{Binding Person.LastName}" />

            <Entry Grid.Row="2"
                   Placeholder="Enter your email address"
                   PlaceholderColor="DarkGoldenrod"
                   FontAttributes="Bold"
                   Text="{Binding Person.Email}" />

            <Entry Grid.Row="3"                                   
                   Placeholder="Enter your password"
                   PlaceholderColor="DarkGoldenrod"
                   FontAttributes="Bold"
                   IsPassword="True"
                   Text="{Binding Person.Password}" />

            <Button Grid.Row="4"  
                    Margin="10"
                    Text="Add to Database"
                    FontAttributes="Bold"
                    HorizontalOptions="Center"
                    Command="{Binding AddPersonToListAndDbCommand}"/>



            <CollectionView Grid.Row="5" 
                            ItemsSource="{Binding Persons}">
                  <CollectionView.ItemTemplate>
                        <DataTemplate>
                              <StackLayout Margin="5">
                                    <HorizontalStackLayout>
                                          <Label Text="First name:" />
                                          <Label Text="{Binding Person.FirstName}" />
                                    </HorizontalStackLayout>
                                    <HorizontalStackLayout>
                                          <Label Text="Last name:" />
                                          <Label Text="{Binding Person.LastName}" />
                                    </HorizontalStackLayout>
                                    <HorizontalStackLayout>
                                          <Label Text="Email address: " />
                                          <Label Text="{Binding Person.Email}" />
                                    </HorizontalStackLayout>
                              </StackLayout>
                        </DataTemplate>
                  </CollectionView.ItemTemplate>
            </CollectionView>
      </Grid>
</ContentPage>

问题很可能出在绑定中,但我不知道还能尝试什么。

调试时,我将鼠标悬停在

Text={Binding Person.FirstName}
上以查看
Person.FirstName
的值。好吧,如果我将鼠标悬停在
Person
上,我可以深入了解该对象,并且所有正确的值都在那里,但是当我将鼠标悬停在
FirstName
上时,它会给出错误
children could not be evaluated

为了说明正在创建

Person
对象列表,这是我创建了一些对象并将它们放入列表后的手表图像:
enter image description here

xaml data-binding binding maui collectionview
1个回答
0
投票

因为您的

ItemsSource
是一个
IEnumerable<Person>
,所以
CollectionView
的每一行都有当前
BindingContext
对象的
Person

这意味着

DataTemplate
中的绑定表达式就是

Text="{Binding FirstName}"

但是,因为您使用的是编译绑定,所以还需要指定

DataType

<DataTemplate x:DataType="model:Person">

您还需要为

xmlns
 添加 
model

声明
© www.soinside.com 2019 - 2024. All rights reserved.