C
C#17mo ago
Kosta

❔ Why do my source generated properties are unreacheable in code? using mvvm toolkit

Hello i got the following code:
public partial class CalculatorVM : ObservableObject
{
[ObservableProperty]
private int buttonOne = 1;

[ObservableProperty]
private int buttonTwo = 2;

[ObservableProperty]
private int buttonThree = 3;

[ObservableProperty]
private int buttonFour = 4;

[ObservableProperty]
private int buttonFive = 5;

[ObservableProperty]
private int buttonSix = 6;

[ObservableProperty]
private int buttonSeven = 7;

[ObservableProperty]
private int buttonEight = 8;

[ObservableProperty]
private int buttonNine = 9;

[ObservableProperty]
private double result, lastNumber;

[ObservableProperty]
SelectedOperator selectedOperator;

[ObservableProperty]
private int clickedNumber;

[ObservableProperty]
private Label resultLabel;



[RelayCommand]
public void NumbercClicked(int number)
{
var selectedNumber = number switch
{
1 => ButtonOne,
2 => ButtonTwo,
3 => ButtonThree,
4 => ButtonFour,
5 => ButtonFive,
6 => ButtonSix,
7 => ButtonSeven,
8 => ButtonEight,
9 => ButtonNine,

};
ClickedNumber = selectedNumber;
if (ClickedNumber == 0) Result = 0;
else Result = Result + selectedNumber;


}
public partial class CalculatorVM : ObservableObject
{
[ObservableProperty]
private int buttonOne = 1;

[ObservableProperty]
private int buttonTwo = 2;

[ObservableProperty]
private int buttonThree = 3;

[ObservableProperty]
private int buttonFour = 4;

[ObservableProperty]
private int buttonFive = 5;

[ObservableProperty]
private int buttonSix = 6;

[ObservableProperty]
private int buttonSeven = 7;

[ObservableProperty]
private int buttonEight = 8;

[ObservableProperty]
private int buttonNine = 9;

[ObservableProperty]
private double result, lastNumber;

[ObservableProperty]
SelectedOperator selectedOperator;

[ObservableProperty]
private int clickedNumber;

[ObservableProperty]
private Label resultLabel;



[RelayCommand]
public void NumbercClicked(int number)
{
var selectedNumber = number switch
{
1 => ButtonOne,
2 => ButtonTwo,
3 => ButtonThree,
4 => ButtonFour,
5 => ButtonFive,
6 => ButtonSix,
7 => ButtonSeven,
8 => ButtonEight,
9 => ButtonNine,

};
ClickedNumber = selectedNumber;
if (ClickedNumber == 0) Result = 0;
else Result = Result + selectedNumber;


}
I cant compile, tells me buttonOne etc dont exsist in the current context, The Repo: https://github.com/KostaKing/Calculator/tree/master/Calculator/ViewModels Anybody has any idea?
GitHub
Calculator/Calculator/ViewModels at master · KostaKing/Calculator
Contribute to KostaKing/Calculator development by creating an account on GitHub.
139 Replies
Kosta
Kosta17mo ago
No ideas? 😄
Anton
Anton17mo ago
where does it say that and lol that code looks cringe idk what's the proper way of doing this but it's kinda cringe nonetheless and what are those properties for buttons for why are you using .net framework?
Google
Google17mo ago
I thought if you wanted to use attributes on the properties you had to use an attribute on the class.
Anton
Anton17mo ago
they have inherited ObservableObject
Google
Google17mo ago
I know. I thought they had to use the attribute instead.
Anton
Anton17mo ago
you can do either one in that library the attribute just mixes in the code that's in that base class
Google
Google17mo ago
inheritance has fewer options and doesn't require partial public class MainWindowViewModel : ObservableObject is what I have right now.
Anton
Anton17mo ago
having code gen for the props requires partial anyway and inheriting generates less code
Google
Google17mo ago
and they want to generate the Command stuff I bet you their code right now says unnecessary partial or whatever.
Anton
Anton17mo ago
anyway, you can switch to the attribute any time
Google
Google17mo ago
Which is what they can do to fix the issue. Which was my first comment.
Anton
Anton17mo ago
unnecessary partial is not an error tho + the issue they mentioned says something else
Google
Google17mo ago
Google
Google17mo ago
Which is weird, because I just tested it out, and it was just what I said
Anton
Anton17mo ago
well just add the partial sure
Google
Google17mo ago
actually, we still don't know what their error message is
Anton
Anton17mo ago
the attribute vs base class is not part of the issue
antimatter8189
antimatter818917mo ago
Hey its me , using a dif pc anyways. code is cringe atm trying to converts a shit code into a normal once, just work in process Cuz i have to old codebase
Anton
Anton17mo ago
You can keep using .net framework, but with sdk-style projects
antimatter8189
antimatter818917mo ago
Did you guys come to a solution? on to why i cant use those properties?
Anton
Anton17mo ago
where's the error
antimatter8189
antimatter818917mo ago
Well it tells me those properties that i defined are not accessible in the current context all of them
Anton
Anton17mo ago
ah I think I caught on to it
antimatter8189
antimatter818917mo ago
the codegen does happen
Anton
Anton17mo ago
well the properties have different names than the fields
antimatter8189
antimatter818917mo ago
you can grab the project its open on github, when u run it and click 1 you'll see
Anton
Anton17mo ago
look at the generated code
antimatter8189
antimatter818917mo ago
Yup they do it makes them capitilized
Anton
Anton17mo ago
they are in pascal case
antimatter8189
antimatter818917mo ago
im using the Cap ones as i should
Anton
Anton17mo ago
I cant compile, tells me buttonOne etc dont exsist in the current context,
antimatter8189
antimatter818917mo ago
acessing the field not the propery yeah but i pasted the code lol you can see im using the propery thats generated not the field if you download the project and rebuild it you'll see the errors in the output but when u f12 the "ButtonOne" it does go into the generated code
Anton
Anton17mo ago
Why are they observable anyway? do you ever change their values? just make getters public int ButtonOne => 1
antimatter8189
antimatter818917mo ago
Nop, but it doesnt matter Result needs to be and it cant locate it either problem is not only buttons, all the props are invisible
Anton
Anton17mo ago
Anton
Anton17mo ago
I wonder why see the problem?
antimatter8189
antimatter818917mo ago
initilazing the datacontex in the back? hmm explain
Anton
Anton17mo ago
swap these lines around
thebeardedquack
thebeardedquack17mo ago
InitializeComponent() will cause UI updates. UI items pull information from the DataContext DataContext does not exist during InitializeComponent()
Anton
Anton17mo ago
it uses the view as the datacontext this
thebeardedquack
thebeardedquack17mo ago
Ew
Anton
Anton17mo ago
I think
antimatter8189
antimatter818917mo ago
Hmm, what?
thebeardedquack
thebeardedquack17mo ago
As far as I'm concerned the View should have no code whatsoever. The ViewModel should have no idea about UI, whatsoever. Everything, is done via binding. The data context for your View, is the ViewModel, and the two are connected via binding
Anton
Anton17mo ago
It uses the current instance of MainWindow as the data context so it's trying to find those properties on this
antimatter8189
antimatter818917mo ago
I did nothing happened
Anton
Anton17mo ago
unless you reset the data context to the view model first
thebeardedquack
thebeardedquack17mo ago
This is MVVM, so you should have a MainWindowView and a MainWindowViewModel
antimatter8189
antimatter818917mo ago
Kinda weak on that still learning can you elaborate on that?
thebeardedquack
thebeardedquack17mo ago
The MainWindowViewModel is the DataContext for a MainWindowView. Your MVVM framework should be able to automatically select the View, when you tell it to display a ViewModel
Anton
Anton17mo ago
well do you know how bindings work even?
antimatter8189
antimatter818917mo ago
depends on what lvl , but yeah basically
Anton
Anton17mo ago
INotifyPropertyChanged? Reflection?
antimatter8189
antimatter818917mo ago
Yes i know those
Anton
Anton17mo ago
Well, InitializeComponent creates your UI tree it creates the bindings it sets the bindings the data context is the object against which the bindings are created
thebeardedquack
thebeardedquack17mo ago
I just looked at your project repo... Where's your views?
antimatter8189
antimatter818917mo ago
there is 1 view, mainview but if i do this
DataContext = new CalculatorVM();

InitializeComponent();
DataContext = new CalculatorVM();

InitializeComponent();
Then the datacontex should be set to vm then initialized
Anton
Anton17mo ago
when a binding for ButtonOne is created, it uses reflection to access the property ButtonOne on the data context, and subs to PropertyChanged of ButtonOne
thebeardedquack
thebeardedquack17mo ago
How does your MVVM framework know that it should connect "MainWindow" which is not named "View" with "CalculatorVM" which is also not named with "ViewModel"?
Anton
Anton17mo ago
well vm is your data context it is supposed to be at least
antimatter8189
antimatter818917mo ago
IDK? learning stage atm but if i manually inject it should know no matter what they called no?
thebeardedquack
thebeardedquack17mo ago
If you're learning, keep it super simple for yourself. Have a MainWindowView.xaml and a MainWindowViewModel.cs
antimatter8189
antimatter818917mo ago
like how do i fix this guys, whats the solution whats the root cause
thebeardedquack
thebeardedquack17mo ago
MVVM usually has some reflection under the hood that gets the class name and replaces "ViewModel" with "View"
antimatter8189
antimatter818917mo ago
Lets say i want to keep it as it is @TheBeardedQuack and i just want to make it work like it is why dont it?
Anton
Anton17mo ago
yeah it's fine
antimatter8189
antimatter818917mo ago
where does the binding fail? I know this isnt proper code just focusing on other issue atm why cant it acess the ButtonOne property
Anton
Anton17mo ago
it should work if you set the data context to the view model
thebeardedquack
thebeardedquack17mo ago
I'm not familiar with your particular MVVM toolkit... Most of what I've seen uses properties, not fields, and doesn't require attribute decorations
Anton
Anton17mo ago
are you sure the code is getting generated? I mean, source generators is a new feature
antimatter8189
antimatter818917mo ago
Yes it is @AntonC , you can click it and see
Anton
Anton17mo ago
I'm not sure they even work with non sdk style projects
antimatter8189
antimatter818917mo ago
f12 it and you;ll go into generated code
Anton
Anton17mo ago
I'm not looking at your code rn I don't wanna clone
antimatter8189
antimatter818917mo ago
sample:
{
/// <inheritdoc cref="buttonOne"/>
[global::System.CodeDom.Compiler.GeneratedCode("CommunityToolkit.Mvvm.SourceGenerators.ObservablePropertyGenerator", "8.1.0.0")]
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
public int ButtonOne
{
get => buttonOne;
set
{
if (!global::System.Collections.Generic.EqualityComparer<int>.Default.Equals(buttonOne, value))
{
OnButtonOneChanging(value);
OnPropertyChanging(global::CommunityToolkit.Mvvm.ComponentModel.__Internals.__KnownINotifyPropertyChangingArgs.ButtonOne);
buttonOne = value;
OnButtonOneChanged(value);
OnPropertyChanged(global::CommunityToolkit.Mvvm.ComponentModel.__Internals.__KnownINotifyPropertyChangedArgs.ButtonOne);
}
}
}

/// <inheritdoc cref="buttonTwo"/>
[global::System.CodeDom.Compiler.GeneratedCode("CommunityToolkit.Mvvm.SourceGenerators.ObservablePropertyGenerator", "8.1.0.0")]
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
public int ButtonTwo
{
get => buttonTwo;
set
{
if (!global::System.Collections.Generic.EqualityComparer<int>.Default.Equals(buttonTwo, value))
{
OnButtonTwoChanging(value);
OnPropertyChanging(global::CommunityToolkit.Mvvm.ComponentModel.__Internals.__KnownINotifyPropertyChangingArgs.ButtonTwo);
buttonTwo = value;
OnButtonTwoChanged(value);
OnPropertyChanged(global::CommunityToolkit.Mvvm.ComponentModel.__Internals.__KnownINotifyPropertyChangedArgs.ButtonTwo);
}
}
}
{
/// <inheritdoc cref="buttonOne"/>
[global::System.CodeDom.Compiler.GeneratedCode("CommunityToolkit.Mvvm.SourceGenerators.ObservablePropertyGenerator", "8.1.0.0")]
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
public int ButtonOne
{
get => buttonOne;
set
{
if (!global::System.Collections.Generic.EqualityComparer<int>.Default.Equals(buttonOne, value))
{
OnButtonOneChanging(value);
OnPropertyChanging(global::CommunityToolkit.Mvvm.ComponentModel.__Internals.__KnownINotifyPropertyChangingArgs.ButtonOne);
buttonOne = value;
OnButtonOneChanged(value);
OnPropertyChanged(global::CommunityToolkit.Mvvm.ComponentModel.__Internals.__KnownINotifyPropertyChangedArgs.ButtonOne);
}
}
}

/// <inheritdoc cref="buttonTwo"/>
[global::System.CodeDom.Compiler.GeneratedCode("CommunityToolkit.Mvvm.SourceGenerators.ObservablePropertyGenerator", "8.1.0.0")]
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
public int ButtonTwo
{
get => buttonTwo;
set
{
if (!global::System.Collections.Generic.EqualityComparer<int>.Default.Equals(buttonTwo, value))
{
OnButtonTwoChanging(value);
OnPropertyChanging(global::CommunityToolkit.Mvvm.ComponentModel.__Internals.__KnownINotifyPropertyChangingArgs.ButtonTwo);
buttonTwo = value;
OnButtonTwoChanged(value);
OnPropertyChanged(global::CommunityToolkit.Mvvm.ComponentModel.__Internals.__KnownINotifyPropertyChangedArgs.ButtonTwo);
}
}
}
its generated alright.
thebeardedquack
thebeardedquack17mo ago
ButtonOne is not a property that exists from what I can see on CalculatorVM.cs buttonOne exists
antimatter8189
antimatter818917mo ago
yeah but thats the field i want to acess the prop
thebeardedquack
thebeardedquack17mo ago
What property?
Anton
Anton17mo ago
they're using MVVM community toolkit
antimatter8189
antimatter818917mo ago
this one:
/// <inheritdoc cref="buttonOne"/>
[global::System.CodeDom.Compiler.GeneratedCode("CommunityToolkit.Mvvm.SourceGenerators.ObservablePropertyGenerator", "8.1.0.0")]
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
public int ButtonOne
{
get => buttonOne;
set
{
if (!global::System.Collections.Generic.EqualityComparer<int>.Default.Equals(buttonOne, value))
{
OnButtonOneChanging(value);
OnPropertyChanging(global::CommunityToolkit.Mvvm.ComponentModel.__Internals.__KnownINotifyPropertyChangingArgs.ButtonOne);
buttonOne = value;
OnButtonOneChanged(value);
OnPropertyChanged(global::CommunityToolkit.Mvvm.ComponentModel.__Internals.__KnownINotifyPropertyChangedArgs.ButtonOne);
}
}
}
/// <inheritdoc cref="buttonOne"/>
[global::System.CodeDom.Compiler.GeneratedCode("CommunityToolkit.Mvvm.SourceGenerators.ObservablePropertyGenerator", "8.1.0.0")]
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
public int ButtonOne
{
get => buttonOne;
set
{
if (!global::System.Collections.Generic.EqualityComparer<int>.Default.Equals(buttonOne, value))
{
OnButtonOneChanging(value);
OnPropertyChanging(global::CommunityToolkit.Mvvm.ComponentModel.__Internals.__KnownINotifyPropertyChangingArgs.ButtonOne);
buttonOne = value;
OnButtonOneChanged(value);
OnPropertyChanged(global::CommunityToolkit.Mvvm.ComponentModel.__Internals.__KnownINotifyPropertyChangedArgs.ButtonOne);
}
}
}
auto generated
thebeardedquack
thebeardedquack17mo ago
I assume that's something handled by your toolkit
Anton
Anton17mo ago
it autogenerates the properties
antimatter8189
antimatter818917mo ago
yup
thebeardedquack
thebeardedquack17mo ago
That looks nasty XD
Anton
Anton17mo ago
it just saves you a bunch of boilerplate
Anton
Anton17mo ago
well paste the error it gives you when you click or whatever
antimatter8189
antimatter818917mo ago
i cant launch the program
Anton
Anton17mo ago
compilation error then
antimatter8189
antimatter818917mo ago
CS0103: The name 'ButtonOne' does not exist in the current context on all of the properties all of them
Anton
Anton17mo ago
in what context
antimatter8189
antimatter818917mo ago
just says current contex
Anton
Anton17mo ago
what line the error is at
antimatter8189
antimatter818917mo ago
CalculatorVM.cs(63,22,63,31)
Anton
Anton17mo ago
I mean, paste the relevant bit here
antimatter8189
antimatter818917mo ago
1 => ButtonOne, [ObservableProperty] private int buttonOne = 1;
Anton
Anton17mo ago
nah it feels like the code doesn't get generated, or your ide doesn't recognize it restart the ide or like the other way your ide generates the code on the side
antimatter8189
antimatter818917mo ago
did multiple times also the pc itself
Anton
Anton17mo ago
but the compiler doesn't recognize it
antimatter8189
antimatter818917mo ago
yup most likely i dont get why
Anton
Anton17mo ago
I mean, source gen is a new feature
antimatter8189
antimatter818917mo ago
yeah but it does source gen which is odd there was a fix for it cuz 4.8 didnt nativley support it
Anton
Anton17mo ago
migrate to sdk style projects you should do it anyway
antimatter8189
antimatter818917mo ago
I cant, not my choice millions of lines project, im a simple bolt
Anton
Anton17mo ago
why not? you don't lose anything
antimatter8189
antimatter818917mo ago
Cuz its not up 2 me
Anton
Anton17mo ago
you don't have to use .net core you can keep using framework it's just that the project file will become simpler
antimatter8189
antimatter818917mo ago
would you tell google rewrite X sofware for my Standars ? no you wouldnt Same thing here
Anton
Anton17mo ago
I'm not telling you to rewrite the code just the project file there should be tools that can do this automatically even
antimatter8189
antimatter818917mo ago
who says that would help anyways? feels like shooting in the dark we have no idea why the source gen isnt working proeprly we have no idea if doing the sdk thing will actually solve anything aye we need some c# / mvvm gods in this thread
Anton
Anton17mo ago
I asked it in #roslyn wait for people to drop by Anyways, what's up with that code anyway why are those observable and why are you mapping them in that switch
Anton
Anton17mo ago
Anton
Anton17mo ago
Is this a ui element I'm seeing here?
antimatter8189
antimatter818917mo ago
btw
antimatter8189
antimatter818917mo ago
Stack Overflow
Community Toolkit MVVM doesn't bind data between View and View Mode...
My ViewModel partial class TestViewModel : ObservableObject { [ObservableProperty] private string inputText; } My View <Window x:Class="WpfApp_framework.MainWindow&...
antimatter8189
antimatter818917mo ago
yeah its irrelevant thats how i made source gen work
Anton
Anton17mo ago
well congrats, you've partially migrated to sdk style
antimatter8189
antimatter818917mo ago
lol I've never developed an SDK dont even know where to begin on that and that benifits it grants me, but it doesnt have anything to do with the issue at hand probably
Anton
Anton17mo ago
They just look like this https://github.com/AntonC9018/uni_csharp/blob/master/sem2_lab1/lab1.csproj I think you should just change the command parameter to a constant and remove those properties altogether
antimatter8189
antimatter818917mo ago
I can change alot of things and make it work i want to know why it doesnt work as is
Anton
Anton17mo ago
well now it does right?
antimatter8189
antimatter818917mo ago
?
Anton
Anton17mo ago
did that error go away?
antimatter8189
antimatter818917mo ago
I did nothing? why would it go away? you want me to copy that github code?
Anton
Anton17mo ago
well you said you solved the problem by adding that package reference
antimatter8189
antimatter818917mo ago
Where did i say that? 😄 problem hasnt been solved at all
Anton
Anton17mo ago
.
antimatter8189
antimatter818917mo ago
same problem presists Code gen yes but not the "out of context" part
Anton
Anton17mo ago
right
antimatter8189
antimatter818917mo ago
Like i said, im getting the codegen even pasted here above i just cant access the generated properties for some odd reason
Anton
Anton17mo ago
Well I think it's because you have to add the generated files to the compilation manually
antimatter8189
antimatter818917mo ago
Cuz its a 4.8? common gotta be a workaround lol if the gen happens
Anton
Anton17mo ago
because you're not using sdk style it would include them manually the workaround, or rather the solution, is to migrate to sdk style
antimatter8189
antimatter818917mo ago
But it does work on .net 6? makes no sense
Anton
Anton17mo ago
like I already told you 5 times
antimatter8189
antimatter818917mo ago
u dont need sdk for .net 6 to make it work i want to make it work on 4.8 simple as that
Anton
Anton17mo ago
idk wait for the experts to wake up in the roslyn channel
antimatter8189
antimatter818917mo ago
Yup lol still here, still stuck 😄
Accord
Accord17mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.