I need help regarding C# Community Toolkit Mvvm Source Generator

Viewed 490

Hi I am trying to implement C# Community Toolkit Mvvm Source Generator in my WPF sample app I have the following code

    private ToDoTask _task;
    public TaskViewModel()
    {
        _task = new ToDoTask();
        _tasks = new ObservableCollection<ToDoTask>();
    }

    [ObservableProperty]
    string title;

    [ObservableProperty]
    string dueDate;

    [ObservableProperty]
    string comment;
    .
    .
    .

C# Community Toolkit Mvvm Source Generator Produces the following results,

public partial class TaskViewModel
{
    [global::System.CodeDom.Compiler.GeneratedCode("CommunityToolkit.Mvvm.SourceGenerators.ObservablePropertyGenerator", "7.1.0.0")]
    [global::System.Diagnostics.DebuggerNonUserCode]
    [global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
    public string Title
    {
        get => title;
        set
        {
            if (!global::System.Collections.Generic.EqualityComparer<string>.Default.Equals(title, value))
            {
                OnPropertyChanging(global::CommunityToolkit.Mvvm.ComponentModel.__Internals.__KnownINotifyPropertyChangedOrChangingArgs.TitlePropertyChangingEventArgs);
                title = value;
                OnPropertyChanged(global::CommunityToolkit.Mvvm.ComponentModel.__Internals.__KnownINotifyPropertyChangedOrChangingArgs.TitlePropertyChangedEventArgs);
            }
        }
    }

    [global::System.CodeDom.Compiler.GeneratedCode("CommunityToolkit.Mvvm.SourceGenerators.ObservablePropertyGenerator", "7.1.0.0")]
    [global::System.Diagnostics.DebuggerNonUserCode]
    [global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
    public string DueDate
    {
        get => dueDate;
        set
        {
            if (!global::System.Collections.Generic.EqualityComparer<string>.Default.Equals(dueDate, value))
            {
                OnPropertyChanging(global::CommunityToolkit.Mvvm.ComponentModel.__Internals.__KnownINotifyPropertyChangedOrChangingArgs.DueDatePropertyChangingEventArgs);
                dueDate = value;
                OnPropertyChanged(global::CommunityToolkit.Mvvm.ComponentModel.__Internals.__KnownINotifyPropertyChangedOrChangingArgs.DueDatePropertyChangedEventArgs);
            }
        }
    }

    [global::System.CodeDom.Compiler.GeneratedCode("CommunityToolkit.Mvvm.SourceGenerators.ObservablePropertyGenerator", "7.1.0.0")]
    [global::System.Diagnostics.DebuggerNonUserCode]
    [global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
    public string Comment
    {
        get => comment;
        set
        {
            if (!global::System.Collections.Generic.EqualityComparer<string>.Default.Equals(comment, value))
            {
                OnPropertyChanging(global::CommunityToolkit.Mvvm.ComponentModel.__Internals.__KnownINotifyPropertyChangedOrChangingArgs.CommentPropertyChangingEventArgs);
                comment = value;
                OnPropertyChanged(global::CommunityToolkit.Mvvm.ComponentModel.__Internals.__KnownINotifyPropertyChangedOrChangingArgs.CommentPropertyChangedEventArgs);
            }
        }
    }
} 
.
.
.

But, I need the following results,

    private ToDoTask _task;
    public TaskViewModel()
    {
        _task = new ToDoTask();            
        _tasks = new ObservableCollection<ToDoTask>();
    }
    #endregion

    #region Properties
    
    public string Title
    {
        get { return _task.Title; }
        set
        {
            _task.Title = value;
            OnPropertyChanged("Title");
        }
    }

    public string DueDate
    {
        get { return _task.DueDate; }
        set
        {
            _task.DueDate = value;
            OnPropertyChanged("DueDate");
        }
    }

    public string Comment
    {
        get { return _task.Comment; }
        set
        {
            _task.Comment = value;
            OnPropertyChanged("Comment");
        }
    }

Can anyone help me in this regard, Is it possible to generate results similar to last one with Community Toolkit Mvvm Source Generator. If, Yes then how!?

1 Answers

It seem like for now, it has to be done manually. Example:

public class TaskViewModel : ObservableObject
{
    private readonly TodoTask _task;

    public TaskViewModel(TodoTask task) => _task = task;

    public string Title
    {
        get => _task.Title;
        set => SetProperty(_task.Title, value, _task, (t, n) => t.Title = n);
    }
}

[EDIT] Earlier answer below because I mixed up mvvmgen and community toolkit mvvm:

Using MvvmGen

The answer (for MvvmGen) is here.

Basically just do this:

[ViewModel(ModelType = typeof(TodoTask))]
 public partial class TaskViewModel() { }

and the properties are generated.

Here is the generated code from those two lines:

partial class TaskViewModel : global::MvvmGen.ViewModels.ViewModelBase
{
    public TaskViewModel()
    {
        this.OnInitialize();
    }

    partial void OnInitialize();

    public string Title
    {
        get => Model.Title;
        set
        {
            if (Model.Title != value)
            {
                Model.Title = value;
                OnPropertyChanged("Title");
            }
        }
    }

//...

    protected WpfApp1.TodoTask Model { get; set; }
}
Related