在我的.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: [
我解释说,我将有可观察的托收收据的数据可视化正确
我试着收集名字等等,但都没有用,我不是很熟练,所以我想在这里问这个问题.