2

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

2 Answers2

0

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; }
}
Magnus
  • 353
  • 3
  • 8
  • Thanks for your guidance dear, but either in Source Generator of Community Toolkit Mvvm this solution doesn't work or I'm Unable to understand, Actually, I'm working with WPF – Muhammad Farhan Jul 06 '22 at 08:55
  • Did you notice I made an error? I had typed `public TaskViewModel() { }` but it should be `public partial class TaskViewModel() { }` – Magnus Jul 06 '22 at 10:15
  • Bro, you are talking about MvvmGen, but I'm finding solution for Community Toolkit Mvvm Source Generator both are different packages, I'm sure your solution must work after adding MvvmGen, but I don't wanna add both packages. Hope you understand my point. – Muhammad Farhan Jul 06 '22 at 11:00
  • "Generate Properties From a Model" Probability this functionality will also be in Community Toolkit Mvvm Source Generator & I'm searching for it's code sample. – Muhammad Farhan Jul 06 '22 at 11:04
  • Oops, I mixed them up, sorry. Seems like this functionality is not in the community toolkit, and has to be done manually. – Magnus Jul 06 '22 at 12:00
0

Unfortunately, you are wanting to implement MVVM incorrectly. If you want the properties in ToDoTask to be observable properties, you should make ToDoTask an ObsevableObject.

bkjames
  • 266
  • 1
  • 3
  • 15