MAUI:ListView 与自定义 ViewCell 的绑定

MAUI: ListView Binding With Custom ViewCell

我使用 FreshMvvm 在 Windows 上开发和 运行 MAUI 项目。

但是我的 ListView 和我的自定义模板存在一些绑定问题。

以下是我的代码:

型号:

public class BaseModel
{
    public string Code{ get; set; }
}

public class NameModel: BaseModel
{
    public string Name{ get; set; }
}

视图模型:

public class MainPageModel : FreshBasePageModel
{
    private readonly IApiService _apiService;
    private List<NameModel> _nameModelList;

    public List<NameModel> NameModelList
    {
        get => _nameModelList;
        private set 
        {
            _nameModelList= value;

            RaisePropertyChanged(nameof(NameModelList));
        }
    }

    public MainPageModel(IApiService apiService)
    {
        _apiService = apiService;
    }

    protected override void ViewIsAppearing(object sender, EventArgs e)
    {
        base.ViewIsAppearing(sender, e);

        Task.Run(() => GetNameData());
    }

    private async Task GetNameData()
    {
        var result = await _apiService.GetNameData();
        NameModelList= result.GetRange(1, 10);
    }
}

我创建了一个列表并使用 api 服务来获取名称模型列表数据。 如果api服务获取数据,NameModelList将被更新。

NameModelList 是将绑定到 Listview.ItemsSource

的 属性

MainPage.xmal:

<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:MyNamespace.ViewCells.CustomListViewCell"
             x:Class="MyNamespace.Pages.MainPage"
             BackgroundColor="{DynamicResource SecondaryColor}">

    <Grid RowSpacing="25" 
          RowDefinitions="Auto" 
          VerticalOptions="FillAndExpand"
          HorizontalOptions="FillAndExpand">

        <ListView 
                x:Name="MyListView"
                ItemsSource="{Binding NameModelList}"
                Grid.Row="0"
                WidthRequest="800"
                HeightRequest="800"
                BackgroundColor="Gray"
                VerticalOptions="FillAndExpand"
                HorizontalOptions="FillAndExpand">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <local:MyCustomViewCell/>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>

    </Grid>
</ContentPage>

自定义 ViewCell (.xml):

<ViewCell xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
          xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
          x:Class="MyNamespace.ViewCells.CustomListViewCell.MyCustomViewCell">

    <Grid RowSpacing="100"  WidthRequest="100" HeightRequest="100">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="100*" />
        </Grid.ColumnDefinitions>
        <StackLayout
            GridLayout.Row="0"
            GridLayout.Column="0">
            <Label
                Text="{Binding Code}"
                FontSize="30"/>
            <Label
                Text="{Binding Name}"
                FontSize="30"/>
        </StackLayout>
    </Grid>
</ViewCell>

自定义 ViewCell (.cs)

public partial class MyCustomViewCell: ViewCell
{
    public static readonly BindableProperty CodeProperty = 
        BindableProperty.Create("Code", typeof(string), typeof(MyCustomViewCell), "");

    public string Code
    {
        get { return (string)GetValue(CodeProperty); }
        set { SetValue(CodeProperty, value); }
    }

    public static readonly BindableProperty NameProperty =
        BindableProperty.Create("Name", typeof(string), typeof(MyCustomViewCell), "");

    public string Name
    {
        get { return (string)GetValue(NameProperty); }
        set { SetValue(NameProperty, value); }
    }
}

我定义了一个自定义的ViewCell文件,并将这个ViewCell放在MainPage的Listview中。

现在我的问题是我的 Listview 无法成功显示数据。

我确定 NameModelList 有价值并且它的计数大于 1。

但是我什么也看不到。

输出日志无错误,MyCustomViewCell.cs中的断点从未触发

所以我想我有一些绑定问题,但我找不到。

为了弄清楚这个问题,我把你的代码放到了一个项目中,这样我就可以玩一下了。您可以找到存储库 here。不要在这里或任何地方粗鲁,但下一个问题可能是一个好主意自己做,这将有助于加快速度:)

总之,问题要微妙得多。因为您在布局中使用 XAML,所以您必须在构造函数中调用 InitializeComponent。所以将它添加到您的 MyCustomViewCell 使其工作:

public MyCustomViewCell()
{
    InitializeComponent();
}