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

k3bvogb1  于 2023-09-28  发布在  .NET
关注(0)|答案(1)|浏览(152)

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

  • 创建一个Person对象,该对象的值来自填充对象属性的字段
  • Person对象添加到ObservableList<Person>中,ObservableList<Person>本身是在ViewModel类的构造函数中创建的

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

我知道正在创建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对象的列表,这里有一个手表的图像,在我创建了一些对象并将它们放入列表之后:

编辑:这是我的MainPage.xaml.cs代码:

using BindingDemo.ViewModel;

namespace BindingDemo
{
    public partial class MainPage : ContentPage
    {
        public MainPage(MainPageViewModel mainPageViewModel)
        {
            InitializeComponent();
            BindingContext = mainPageViewModel;
        }
    }
}
insrf1ej

insrf1ej1#

因为您的ItemsSourceIEnumerable<Person>,所以CollectionView的每一行都有当前Person对象的BindingContext
这意味着DataTemplate中的绑定表达式将相对于Person对象,即

Text="{Binding FirstName}"

但是,由于使用的是编译绑定,因此还需要指定DataType

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

还需要为model添加xmlns声明

相关问题