0

I'm using Microsoft MVVM Toolkit 7.1.2 in a WPF .Net Core 3.1 project. If I use the ObservableObject class it works fine, but, whenever I try to create an ObservableValidator class in my application it gives me several errors in auto generated source files:

Error messages

Even in an test class like really simple:

public class Test : ObservableValidator
{

    private decimal _internalProp;
    public decimal ExternalProp
    {
        get { return _internalProp; }
        set { SetProperty(ref _internalProp, value); }
    }

}

I can't find what is causing this error!

This is the auto generated source files that gives the errors, linked in the error messages:

// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
#pragma warning disable
namespace Microsoft.Toolkit.Mvvm.ComponentModel.__Internals
{
    [global::System.CodeDom.Compiler.GeneratedCode("Microsoft.Toolkit.Mvvm.SourceGenerators.ObservableValidatorValidateAllPropertiesGenerator", "7.1.0.0")]
    [global::System.Diagnostics.DebuggerNonUserCode]
    [global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
    [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)]
    [global::System.Obsolete("This type is not intended to be used directly by user code")]
    internal static partial class __ObservableValidatorExtensions
    {
        [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)]
        [global::System.Obsolete("This method is not intended to be called directly by user code")]
        public static global::System.Action<object> CreateAllPropertiesValidator(global::Sid.Models.Test _)
        {
            static void ValidateAllProperties(object obj)
            {
                var instance = (global::Sid.Models.Test)obj;
            }

            return ValidateAllProperties;
        }
    }
}

2 Answers2

2

i think this is known issue atm, see for workaround https://github.com/CommunityToolkit/dotnet/issues/343

  • Perfect! The global.json trick did it! – Rafael Leonardi Jul 13 '22 at 23:06
  • Link only answers are discouraged since the links can break, and having to go elsewhere to get the information can be tedious. Answers need to have sufficient information to understand them, and should be described in a reasonable level of detail and / or include a minimal example of the answer. – Juanjo Martinez Jul 15 '22 at 12:21
1

As Juanjo Martinez pointed out, the link may be broke someday so just resuming the anwer provided by Tum4ik here https://github.com/dotnet/wpf/issues/6792#issuecomment-1183471011:

  1. Download and install the previous version of .NET SDK (6.0.301) https://dotnet.microsoft.com/en-us/download/dotnet/6.0

My comment = Uninstall other newest installed versions before!

  1. Add global.json file to the root of your solution (where .sln file is located)
  2. The content of the global.json:
{
    "sdk": {
        "version": "6.0.301",
        "rollForward": "disable"
    }
}
  1. Rebuild solution.

This worked for me!

  • The answer that adds the section to the .csproj file worked for me, and does not require downloading and installing another version of the SDK.: – redtetrahedron Sep 06 '22 at 02:08
  • I was having the same duplication issues, but the global.json file in sln directory worked perfectly. Thank you so much – d212digital Sep 06 '22 at 07:37