获取XFC0045 MAUI xaml页面错误“绑定:与mvvm进行数据绑定时,在“Notas.Models.Note”上找不到属性“NoteDescription

gc0ot86w  于 2022-12-25  发布在  其他
关注(0)|答案(1)|浏览(161)

我正在使用.Net 7。我也尝试过删除xaml页面上的x:DataType属性,但错误仍然存在。以下是我的代码
我的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="Notas.View.NotesPage"
             xmlns:model="clr-namespace:Notas.Model"
             xmlns:viewmodel="clr-namespace:Notas.ViewModel" 
             x:DataType="viewmodel:NotesViewModel"
             Title="{Binding Title}">
    
    <VerticalStackLayout HorizontalOptions="Start" Padding="5" Spacing="10" Margin="5">
        <Label Text="My Notes" FontAttributes="Bold" FontSize="Subtitle"/>
        <RefreshView 
            IsRefreshing="{Binding Isrefreshing}"
            Command="{Binding GetNotesCommand}">
        <CollectionView ItemsSource="{Binding Notes}">
            <CollectionView.ItemTemplate>
                <DataTemplate x:DataType="model:Note">
                    <Frame HeightRequest="80">
                        <Label Text="{Binding NoteDescription}"/>
                    </Frame>
                </DataTemplate>
            </CollectionView.ItemTemplate>
        </CollectionView>
        </RefreshView>
    </VerticalStackLayout>
</ContentPage>

我的视图模型页面

using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using Notas.Model;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Notas.ViewModel
{
    public partial class NotesViewModel : BaseViewModel
    {
        public ObservableCollection<Note> Notes { get; private set; } = new ObservableCollection<Note>();
        public NotesViewModel()
        {
            Title = "My Notes";
            GetNotes().Wait();
        }
        
        [ObservableProperty]
        bool isrefreshing;

        [ObservableProperty]
       public string noteText;

        [RelayCommand]
        async Task GetNotes()
        {
            if (IsBusy) return;
       
            try
            { 
                IsBusy = true;
                var notes = App.noteService.getNotes();
                foreach(var note in notes) notes.Add(note);
            }
            catch (Exception)
            {
                await Shell.Current.DisplayAlert("Notas", "Could not fetch list of notes", "OK");
            }
            finally { IsBusy = false; isrefreshing = false; }
        }

        [RelayCommand]
        async Task AddNote()
        {
            if (!string.IsNullOrWhiteSpace(noteText))
            {
                var note = new Note();
                note.NoteDescription = noteText;
                //call service

                await Shell.Current.DisplayAlert("Success", App.noteService.statusMsg, "OK");
                await GetNotes();
            }
            else
            {
                await Shell.Current.DisplayAlert("Notas", "Please enter note description", "OK");
            }
        }
    }
}

我的模型页面

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Notas.Model
{
    [Table("Notes")]
    public class Note : BaseEntity
    {
        [MaxLength(250)]
        public string NoteDescription;
    }
}
93ze6v8z

93ze6v8z1#

只能绑定到公共属性NoteDescription不是C#属性。它应该是

public string NoteDescription { get; set; }

相关问题