Home > Software design >  Getting XFC0045 MAUI xaml page error 'Binding: Property "NoteDescription" not found o
Getting XFC0045 MAUI xaml page error 'Binding: Property "NoteDescription" not found o

Time:12-25

I am using .Net 7. I have also tried to remove x:DataType property on xaml page but still the error persists. Below is my code

My Xaml page

<?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>

My viewmodelpage

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");
            }
        }
    }
}

My model page

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;
    }
}

CodePudding user response:

You can only bind to public properties. NoteDescription is not a C# property. It should be

public string NoteDescription { get; set; }
  • Related