`ListView`中`Label`的内容不显示

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

我想编辑 MAUI 初始化文件以执行以下操作

  1. 每次单击 Button 时,都会向 MainPage.xaml.cs 中的 BindingContext 添加一个对象
  2. 使用 ListView 显示 BindingContext

这样做,就出现了上述问题。具体来说,下图就是发生的事情。

代码如下,我们创建了

Meeting.cs
AllMeetings.cs
作为Models。

MainPage.xaml.cs

namespace App.Views;

using App.Models;

public partial class MainPage : ContentPage
{
    int count = 0;

    public MainPage()
    {
        InitializeComponent();
        BindingContext = new Models.AllMeetings();
    }

    private void OnCounterClicked(object sender, EventArgs e)
    {
        count++;

        if (count == 1)
            CounterBtn.Text = $"Clicked {count} time";
        else
            CounterBtn.Text = $"Clicked {count} times";

        SemanticScreenReader.Announce(CounterBtn.Text);

        ((Models.AllMeetings)BindingContext).Add_Meeting(new Models.Meeting() { Name = "foo" });
    }
}

MainPage.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"
             xmlns:local="clr-namespace:App.Views"
             x:Class="App.Views.MainPage">

    <ScrollView>
        <VerticalStackLayout
            Spacing="25"
            Padding="30,0"
            VerticalOptions="Center">

            <Button
                x:Name="CounterBtn"
                Text="Click me"
                SemanticProperties.Hint="Counts the number of times you click"
                Clicked="OnCounterClicked"
                HorizontalOptions="Center" />

            <ListView ItemsSource="{Binding Meetings}" >
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <ViewCell>
                            <Label Text="{Binding Name}" />
                        </ViewCell>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>

        </VerticalStackLayout>
    </ScrollView>

</ContentPage>

AllMeetings.cs

using System.Collections.ObjectModel;

namespace App.Models;

internal class AllMeetings
{
    public ObservableCollection<Meeting> Meetings { get; set; } = new ObservableCollection<Meeting>();

    public void Add_Meeting(Meeting meeting)
    {
        this.Meetings.Add(meeting);
    }

}

Meeting.cs

namespace App.Models;

internal class Meeting
{
    public string Name { get; set; };
}

当我编写这段代码时,没有输出任何字符,如上图所示,因此当我如下更改

ListView
MainPage.xaml
的内容时,也发生了同样的事情。

            <ListView ItemsSource="{Binding Meetings}" >
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <ViewCell>
                            <Label Text="hoge" />
                        </ViewCell>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>
c# maui
1个回答
0
投票

我已经太晚了,但这可能会帮助遇到此问题的人。

添加父容器例如。标签的 StackLayout

<Stacklayout>
  <Label Text="{Binding Name}" />
</Stacklayout>

如果问题仍然存在。 检查此网址:绑定

将 XAML 文件中根节点上的 ContentPage 或 ContentView 的 x:DataType 属性设置为 View 的 BindingContext 的底层数据类型

<ContentPage
  x:Class="MauiSamples.Views.BindingsPage"
  xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
  xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
  xmlns:viewModels="clr-namespace:MauiSamples.ViewModels"
  x:DataType="viewModels:BindingsViewModel">

在视图层次结构中的任意位置重新定义 x:DataType。为此,我们需要做的就是将 BindingItem 类指定为 DataTemplate 上的 x:DataType,然后绑定将按预期工作

<DataTemplate x:DataType="models: Meeting">
  <ViewCell>
    <Label Text="{Binding Name}" />
  </ViewCell>
</DataTemplate>
© www.soinside.com 2019 - 2024. All rights reserved.