Why does [ObservableProperty] require partial property in some cases but not in others?
              
              #2748
            
            Replies: 2 comments
-
| 
         You need to set your LangVersion to preview in order to use partial properties. Inside your csproj file you need to add the following inside your  <LangVersion>preview</LangVersion> | 
  
Beta Was this translation helpful? Give feedback.
-
| 
         The classes do indeed have to be marked as partial. And, also, you need LangVersion preview features. To get an understanding of what  // PageViewModel.public.cs
public partial class PageViewModel : ObservableObject
{
    public partial HtmlWebViewSource? SourceData { get; set; } = null;
}// PageViewModel.implementation.cs
public partial class PageViewModel : ObservableObject
{
    public partial HtmlWebViewSource? SourceData
    {
        get => field;
        set
        {
            field = value;
            OnPropertyChanged(nameof(SourceData));
        }
    }
}Here we see the following things: 
 By using  You may also like to look at my   | 
  
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
❓ Question
I'm encountering an inconsistency with
[ObservableProperty]fromCommunityToolkit.Mvvm.In many of my view models, I have private fields like this:
...and they work perfectly fine. However, in one case, I'm getting the following compiler error:
I changed the declaration to a property just to check:
...but that gives the same error: it now expects a matching implementation, which of course isn’t written manually.
After reading the docs and examining the working code, I realized:
partialdeclared, yet[ObservableProperty]works.partialto resolve the issue.🤔 My Question(s):
Why does this error only happen for this property, while others using
[ObservableProperty]work fine?Are there conditions under which
[ObservableProperty]requires the class to bepartial, and others where it doesn't?What is the meaning of the suggestion:
💡 What I've Tried:
CommunityToolkit.Mvvmcorrectly.partial— and addingpartialfixed the issue.partial, yet[ObservableProperty]compiles fine.🔧 Version Info:
v8.4.0latest9.0.300Beta Was this translation helpful? Give feedback.
All reactions