Search code examples
c#xamarin.formsinotifypropertychangedmaui

set ObservableProperty in BaseViewModel


I am trying to set ObservableProperty with binding in my ViewModel that inherits from BaseViewModel class in my MAUI app. But when I ran the app. I get this error:

cannot convert from system.componentModel.propertyChangeEvent Args to string  

this is my code:


using Microsoft.Toolkit.Mvvm.ComponentModel;
using Microsoft.Toolkit.Mvvm.Input;
using MvvmHelpers;
using System.Collections.ObjectModel;
using System.Diagnostics;

public partial class DesktopViewModel : BaseViewModel
{
        readonly MesageService messageService;
        public DesktopViewModel(MessageService messageService)
        {
            this.messageService = messageService;
            GetMessageAsync();
        }
        public ObservableCollection<Message> messages { get; } = new();

        [ObservableProperty]
        private string name; 

        [ICommand]
        async Task GetNeprebranaSporocilaAsync()
        {
           if (IsBusy)
                return;
   
            IsBusy = true;
            var msges = await messageService.GetMessageAsync();
            name = "someName";

              foreach (var msg in msges )
                {
                    messages .Add(msg);
                }
        }
}

I get this error only when I add [ObservableProperty] if I change name to uppercase "Name" I get error

The type 'DesktopViewModel' already contains a definition for 'Name'

what am I doing wrong?


Solution

  • I have checked the MvvmHelpers package, it had stopped updating long time ago. In addition, you don't need to use both the MvvmHelpers package and the CommunityToolkit.Mvvm package in your project.

    You can just choose one of them and I suggest you just use the CommunityToolkit.Mvvm, it's easily to use with the Maui projec. So you can just change the viewmodel as:

    [INotifyPropertyChanged]
    public partial class DesktopViewModel 
    {
        readonly MesageService messageService;
        public DesktopViewModel(MessageService messageService)
        {
            this.messageService = messageService;
            GetMessageAsync();
        }
        public ObservableCollection<Message> messages { get; } = new();
    
        [ObservableProperty]
        private string name; 
    
        [ICommand]
        async Task GetNeprebranaSporocilaAsync()
        {
           if (IsBusy)
                return;
    
            IsBusy = true;
            var msges = await messageService.GetMessageAsync();
            name = "someName";
    
              foreach (var msg in msges )
                {
                    messages .Add(msg);
                }
        }
    }
    

    In addition, make the Model named Message extend the ObservableProject, such as:

    public class Message : ObservableObject
    {
    private string name;
    
    public string Name
    {
        get => name;
        set => SetProperty(ref name, value);
    }
    }
    

    It seems when you use the two package the same time, it will declare the same ObservableProperty twice. So you get the error message.