在我的.NET Maui应用程序中显示数据时遇到了困难.在我的Feed页面上,我使用CollectionView控件来显示ReceiptsViewModel类中的数据集合.我有一个收款模型,我希望数据与此模型相关联并显示在我的CollectionView中.但是,尽管正确填充了数据集合,并且My Model和ReceiptsViewModel中的属性也是正确的,但屏幕上没有显示任何内容.

饲料:

public partial class Feed : ContentPage
{
    public Feed(ReceiptsViewModel rvm)
    {
        InitializeComponent();
        BindingContext = rvm;
    }
}

收据型号:

namespace Messenger.Model
{
    public class Receipt
    {
        public string Name { get; set; }
        public string Photo_URL { get; set; }
        public string Description { get; set; }
        public Dictionary<string, object> Ingredients { get; set; }
        public string Cooking { get; set; }
        public string Author { get; set; }

        public Receipt(string name, string photo_URL, string description, Dictionary<string, object> ingredients, string cooking, string author)
        {
            Name = name;
            Photo_URL = photo_URL;
            Description = description;
            Ingredients = ingredients;
            Cooking = cooking;
            Author = author;
        }
    }
}

ReceiptsViewModel:

namespace Messenger.ViewModel
{
    public partial class ReceiptsViewModel : ObservableObject
    {
        [ObservableProperty]
        ObservableCollection<Receipt> receipts;

        [ObservableProperty]
        string receipt_name = "";

        [ObservableProperty]
        string receipt_description = "";

        [ObservableProperty]
        string receipt_author = "";

        [ObservableProperty]
        string receipt_cooking = "";

        [ObservableProperty]
        string receipt_photo = "";

        [ObservableProperty]
        Dictionary<string, object> recepient_ingredients;

        public ReceiptsViewModel()
        {
            Receipts = new ObservableCollection<Receipt>();
        }

        [RelayCommand]
        void Add()
        {
            var receipt = new Receipt(Receipt_name, Receipt_photo, Receipt_description, Recepient_ingredients, Receipt_cooking, Receipt_author);

            Receipts.Add(receipt);
        }
    }
}

Feed.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"
             x:Class="Messenger.Feed"
             Title="Feed"
             xmlns:viewmodel ="clr-namespace:Messenger.ViewModel"
             x:DataType="viewmodel:ReceiptsViewModel">

    <Grid RowDefinitions="100, Auto, *"
          ColumnDefinitions=".75*, .25*">
        <Label Grid.ColumnSpan="2"
               HorizontalTextAlignment="Center"
               VerticalTextAlignment="Center"
               Text="Receipts"
               FontSize="40"
               BackgroundColor="AntiqueWhite"
               x:Name="l"/>
        <Button Text="+"
                HorizontalOptions="End"
                VerticalOptions="Center"
                BackgroundColor="Black"
                Grid.Row="0"
                Margin="15"
                Grid.ColumnSpan="2"
                Command="{Binding AddCommand}"/>
        <CollectionView Grid.Row="2" Grid.ColumnSpan="2" Margin="0,15,0,0"
                        ItemsSource="{Binding Receipts}">
            <CollectionView.ItemTemplate>
                <DataTemplate>
                    <Grid Padding="0,5">
                        <Frame Margin="3,0,5,3" BorderColor="Black">
                            <HorizontalStackLayout>
                                <Image MaximumHeightRequest="100" MaximumWidthRequest="100" Source="dish.jpg"/>
                                <VerticalStackLayout Margin="20,0,0,0" VerticalOptions="Center">
                                    <Label Text="{Binding Receipt_name}" VerticalTextAlignment="Center" LineBreakMode="WordWrap" FontAttributes="Bold"/>
                                    <Label Text="{Binding Receipt_description}" TextColor="LightSlateGray" LineBreakMode="WordWrap"/>
                                </VerticalStackLayout>
                            </HorizontalStackLayout>
                        </Frame>
                    </Grid>
                </DataTemplate>
            </CollectionView.ItemTemplate>
        </CollectionView>
    </Grid>
</ContentPage>

(no)Result image: [Result

我解释说,我将有可观察的托收收据的数据可视化正确

我试着收集名字等等,但都没有用,我不是很熟练,所以我想在这里问这个问题.

推荐答案

CollectionView.ItemTemplate年代S的装订语境是ItemSource's item.因此,您应该绑定property of the Receipt类,而不是ViewModel的属性.例如:

<Label Text="{Binding Name}" VerticalTextAlignment="Center" LineBreakMode="WordWrap" FontAttributes="Bold"/>
<Label Text="{Binding Description}" TextColor="LightSlateGray" LineBreakMode="WordWrap"/>

有关更多信息,您可以参考official collectionview sample.

Csharp相关问答推荐

C# Json重新初始化动态类型

EF Core Fluent API中定义的多对多关系

在C#中使用in修饰符

需要澄清C#的Clean Architecture解决方案模板的AuditableEntityInterceptor类

如何在C#中将对象[*,*]直接转换为字符串[*,*]?

从应用程序图API调用访问所有者字段

单行上的ReSharper数据标注

如何在用户在线时限制令牌生成?

为什么@rendermode Interactive Auto不能在.NET 8.0 Blazor中运行?

HelperText属性不支持复杂内容(混合C#和标记)

为什么我的伺服电机不动,下面的代码?

WPF动态设置弹出窗口水平偏移

如何从Entity Framework Core中填充ListIInterface

如何在发布NuGet包之前设置命名空间?

如何在.NET Core 8中自定义标识用户模型

如何将 colored颜色 转换为KnownColor名称?

最小API定义的Swagger标头参数

为什么C#中的类型别名不能在另一个别名中使用?

MudBlazor MudTabs-->;选项卡内容高度

C#如何替换两个XML元素值?