❔ Why do my source generated properties are unreacheable in code? using mvvm toolkit
Hello i got the following code:
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
No ideas? 😄
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?
I thought if you wanted to use attributes on the properties you had to use an attribute on the class.
they have inherited
ObservableObject
I know. I thought they had to use the attribute instead.
you can do either one in that library
the attribute just mixes in the code that's in that base class
inheritance has fewer options and doesn't require
partial
public class MainWindowViewModel : ObservableObject
is what I have right now.having code gen for the props requires partial anyway
and inheriting generates less code
and they want to generate the
Command
stuff
I bet you their code right now says unnecessary partial
or whatever.anyway, you can switch to the attribute any time
Which is what they can do to fix the issue.
Which was my first comment.
unnecessary partial is not an error tho
+ the issue they mentioned says something else
Which is weird, because I just tested it out, and it was just what I said
well just add the partial sure
actually, we still don't know what their error message is
the attribute vs base class is not part of the issue
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
You can keep using .net framework, but with sdk-style projects
Did you guys come to a solution? on to why i cant use those properties?
where's the error
Well it tells me those properties that i defined are not accessible in the current context
all of them
ah I think I caught on to it
the codegen does happen
well the properties have different names than the fields
you can grab the project its open on github, when u run it and click 1 you'll see
look at the generated code
Yup they do
it makes them capitilized
they are in pascal case
im using the Cap ones
as i should
I cant compile, tells me buttonOne etc dont exsist in the current context,
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
Why are they observable anyway?
do you ever change their values?
just make getters
public int ButtonOne => 1
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
I wonder why
see the problem?
initilazing the datacontex in the back?
hmm explain
swap these lines around
InitializeComponent() will cause UI updates.
UI items pull information from the DataContext
DataContext does not exist during InitializeComponent()
it uses the view as the datacontext
this
Ew
I think
Hmm, what?
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
It uses the current instance of MainWindow as the data context
so it's trying to find those properties on
this
I did nothing happened
unless you reset the data context to the view model first
This is MVVM, so you should have a MainWindowView and a MainWindowViewModel
Kinda weak on that still learning
can you elaborate on that?
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
well do you know how bindings work even?
depends on what lvl , but yeah basically
INotifyPropertyChanged
? Reflection?Yes
i know those
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
I just looked at your project repo... Where's your views?
there is 1 view, mainview
but if i do this
Then the datacontex should be set to vm
then initialized
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
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"?
well vm is your data context
it is supposed to be at least
IDK? learning stage atm
but if i manually inject
it should know
no matter what they called no?
If you're learning, keep it super simple for yourself.
Have a
MainWindowView.xaml
and a MainWindowViewModel.cs
like how do i fix this guys, whats the solution
whats the root cause
MVVM usually has some reflection under the hood that gets the class name and replaces "ViewModel" with "View"
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?
yeah it's fine
where does the binding fail?
I know this isnt proper code
just focusing on other issue atm
why cant it acess the ButtonOne property
it should work if you set the data context to the view model
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
are you sure the code is getting generated?
I mean, source generators is a new feature
Yes it is @AntonC , you can click it and see
I'm not sure they even work with non sdk style projects
f12 it and you;ll go into generated code
I'm not looking at your code rn
I don't wanna clone
sample:
its generated alright.
ButtonOne is not a property that exists from what I can see on
CalculatorVM.cs
buttonOne
existsyeah but thats the field
i want to acess the prop
What property?
they're using MVVM community toolkit
this one:
auto generated
I assume that's something handled by your toolkit
it autogenerates the properties
yup
That looks nasty XD
it just saves you a bunch of boilerplate
ObservableObject - .NET Community Toolkit
A base class for objects of which the properties must be observable
well paste the error it gives you when you click
or whatever
i cant launch the program
compilation error then
CS0103: The name 'ButtonOne' does not exist in the current context
on all of the properties
all of them
in what context
just says current contex
what line
the error is at
CalculatorVM.cs(63,22,63,31)
I mean, paste the relevant bit here
1 => ButtonOne,
[ObservableProperty]
private int buttonOne = 1;
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
did multiple times
also the pc itself
but the compiler doesn't recognize it
yup most likely
i dont get why
I mean, source gen is a new feature
yeah but it does source gen which is odd
there was a fix for it cuz 4.8 didnt nativley support it
migrate to sdk style projects
you should do it anyway
I cant, not my choice
millions of lines project, im a simple bolt
why not? you don't lose anything
Cuz its not up 2 me
you don't have to use .net core
you can keep using framework
it's just that the project file will become simpler
would you tell google rewrite X sofware for my Standars ?
no you wouldnt
Same thing here
I'm not telling you to rewrite the code
just the project file
there should be tools that can do this automatically even
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
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
Is this a ui element I'm seeing here?
btw
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&...
yeah its irrelevant
thats how i made source gen work
well congrats, you've partially migrated to sdk style
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
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
I can change alot of things and make it work
i want to know why it doesnt work as is
well now it does right?
?
did that error go away?
I did nothing? why would it go away?
you want me to copy that github code?
well you said you solved the problem by adding that package reference
Where did i say that? 😄
problem hasnt been solved at all
.
same problem presists
Code gen yes but not the "out of context" part
right
Like i said, im getting the codegen even pasted here above
i just cant access the generated properties
for some odd reason
Well I think it's because you have to add the generated files to the compilation manually
Cuz its a 4.8? common gotta be a workaround lol
if the gen happens
because you're not using sdk style
it would include them manually
the workaround, or rather the solution, is to migrate to sdk style
But it does work on .net 6?
makes no sense
like I already told you 5 times
u dont need sdk for .net 6 to make it work
i want to make it work on 4.8
simple as that
idk
wait for the experts to wake up in the roslyn channel
Yup lol
still here, still stuck 😄
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.