Skip to content

Problems with MVVM Toolkit #846

@RedX2501

Description

@RedX2501

Fody: 6.5.1
PropertyChanged.Fody: 3.4.0

Describe the issue

The following code

    [ObservableObject]
    public partial class AnyViewModel
    {
        [ObservableProperty]
        private string someField = "hey";
    }

Generates the following code:

[GeneratedCode("Microsoft.Toolkit.Mvvm.SourceGenerators.ObservablePropertyGenerator", "7.1.0.0")]
[DebuggerNonUserCode]
[ExcludeFromCodeCoverage]
public string SomeField
{
	get
	{
		return someField;
	}
	set
	{
		if (!EqualityComparer<string>.Default.Equals(someField, value))
		{
			OnPropertyChanging(__KnownINotifyPropertyChangedOrChangingArgs.SomeFieldPropertyChangingEventArgs);
			someField = value;
			OnPropertyChanged(<>PropertyChangedEventArgs.SomeField);
			OnPropertyChanged(__KnownINotifyPropertyChangedOrChangingArgs.SomeFieldPropertyChangedEventArgs);
		}
	}
}
  1. Any assignment of different values to someField will generate a duplicate PropertyChanged event.

Settings [PropertyChanged.DoNotNotify] on the field has no effects (obviously).

Minimal Repro

  1. Copy paste the example above in any WPF project using Microsoft.MVVM.Toolkit.
  2. Compile.
  3. Look at the IL.

Make an effort to fix the bug

Maybe the generator should see that a (oddly) placed call to the OnPropertyChanged already exists and therefore not emit one?

Or should [PropertyChanged.DoNotNotify] be extended to also work on fields? For some reason the flags already indicate that it can be set on fields but it has no effect.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions