Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ObservableProperty] - error CS0103: The name '{Property}' does not exist in the current context #330

Closed
1 of 4 tasks
lemonlad opened this issue Jul 1, 2022 · 2 comments
Labels
external ⤴️ Something related to or caused by an external project mvvm-toolkit 🧰 Issues/PRs for the MVVM Toolkit

Comments

@lemonlad
Copy link

lemonlad commented Jul 1, 2022

Describe the bug

I receive the build-time error:

CS0103: The name '{Property}' does not exist in the current context upon building when using [ObservableProperty] and trying to set the generated property

namespace WpfApp1_TestMVVMToolKit.ViewModels
{
    public partial class MainWindowViewModel : ObservableObject
    {
        [ObservableProperty]
        private string title;

        private void DoSomething()
        {

            Title = "a";
        }
    }

}

Regression

No response

Steps to reproduce

  • Create WPF App (.NET Framework 4.7.2)
  • Add package CommunityToolkit.Mvvm 8.0.0-preview4
  • Add below code to a class
namespace WpfApp1_TestMVVMToolKit.ViewModels
{
    public partial class MainWindowViewModel : ObservableObject
    {
        [ObservableProperty]
        private string title;

        private void DoSomething()
        {

            Title = "a";
        }
    }

}

Build, build fails with error:

error CS0103: The name 'Title' does not exist in the current context

Intellisense does not pick up that Title property exists

Roll back to CommunityToolkit.Mvvm 7.1.2

Build, fails with error:

error CS0103: The name 'Title' does not exist in the current context

In 7.1.2, although build still fails, intellisense does pick up that the Title property exists, with generated code as below:

namespace WpfApp1_TestMVVMToolKit.ViewModels
{
    public partial class MainWindowViewModel
    {
        [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);
                }
            }
        }
    }
}

Expected behavior

I expect to be able to use the generated Title property, and for OnPropertyChanged to fire automatically when changing the value

Screenshots

No response

IDE and version

VS 2022

IDE version

17.2.3

Nuget packages

  • CommunityToolkit.Common
  • CommunityToolkit.Diagnostics
  • CommunityToolkit.HighPerformance
  • CommunityToolkit.Mvvm (aka MVVM Toolkit)

Nuget package version(s)

7.1.2, 8.0.0-preview4

Additional context

No response

Help us help you

No, just wanted to report this

@lemonlad lemonlad added the bug 🐛 An unexpected issue that highlights incorrect behavior label Jul 1, 2022
@Sergio0694
Copy link
Member

Duplicate of #158, this is a known issue with .NET Framework non-SDK-style WPF projects.
You'll have to either move that code to a .NET Standard library, or upgrade to an SDK-style .NET 6 WPF project.

@Sergio0694 Sergio0694 closed this as not planned Won't fix, can't repro, duplicate, stale Jul 1, 2022
@Sergio0694 Sergio0694 added external ⤴️ Something related to or caused by an external project mvvm-toolkit 🧰 Issues/PRs for the MVVM Toolkit and removed bug 🐛 An unexpected issue that highlights incorrect behavior labels Jul 1, 2022
@mklemarczyk
Copy link

mklemarczyk commented Dec 14, 2024

@Sergio0694 it does not work in last .NET 9 with project in SDK style WPF project.

Because of issue dotnet/wpf#10068

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
external ⤴️ Something related to or caused by an external project mvvm-toolkit 🧰 Issues/PRs for the MVVM Toolkit
Projects
None yet
Development

No branches or pull requests

3 participants