Wpf won't compile because SomeClass do not exist in SomeNamespace (but it actually exist!) (solved)
I have Wpf app that don't want to compile after adding Window.DataContext tag pointing to viewmodel class.
The compiler error message says The name SomeVMName does not exist in the namespace SomeNamespaceName (error code XDG0008)
but it is very clear that SomeVMName obviously defined in SomeNamespaceName.
Previously it works fine, the compiler can find SomeVMName in the namespace. After some modifications, the compiler can't find SomeVMName anymore so i'm guessing it's the compiler bug or whatnot.
I have tried restarting visual studio, cleaning the solution (and even deleting the bin and obj folder) but the compilation error still persist. What should i do now? I don't want to rewrite the whole project from scratch. This wpf app may be tiny but i do have another wpf app that does have exactly same problem as this one.
My code is in the screenshot.
SomeNamespaceName is WpfApp3.ViewModels
SomeVMName is WpfApp3.ViewModels.MainViewModel
19 Replies
XDG
are designer errors. They are frequently inaccurate. I suggest you build the project, check the Output window in VS for real compiler errors, and then run the program as normal.
I recommend people to disable the WPF Designer as soon as they're semi-comfortable with XAML because of these ghost errors, poor XAML generation, and worsened VS performance. I prefer running the app and designing at runtime by using XAML Hot Reload.tried rebuild and it still persist and yeah i don't use the designer (i dont drag & drop controls or anything, just use it for preview purposes), i only use the XAML
Is
MainViewModel
in a different project/assembly than the view (XAML)?
Sorry, meant the VMno, both VM and the view are exactly in same project/assembly, just in different namespace
let me try moving them both in same namespace and see if prob still persist
okay, i have done moving both the VM and the view in same namespace and it still dont want to build
It looks ok to me. Let me try to repro...in the meantime try:
1. Clean solution
2. Close VS
3. Delete bin/obj and the .vs folder
4. Open VS, rebuild
Also, what runtime is this?
that's the exact possible solution i have tried
You didn't mention deleting the
.vs
folder...which is also important.i'm targeting .net 6 for this project
yeah i mean i have deleted the .vs folder but forgot to edit the description above
Ah, I see.
No issue here getting it to run with VS 17.3.1 and SDK 6.0.400. Which is not surprising because I've done similar many times before.
Though I typically don't set a
DataContext
from XAML...I prefer ViewModel-First instead of View-First.
Updating VS and will try again. 🤷♂️i have tried rewriting (and copying) the whole thing in new project (or solution) and this is ridiculous... it compile in this new project
Yeah, I've never came across the error where the cause wasn't immediately obvious (a real error) or a ghost error from the designer.
i'm guessing the compiler uses the cached files or whatnot, and in this new project, the compiler use fresh files and such
That's what deleting bin/obj/.vs should solve.
well then, i will try to update VS instead and if i can, i could report the bug to .net developer. Thanks for taking your time helping me, i appreciate it
👍
It still builds for me in the updated version btw, but I didn't recreate the project anew. Only did the cleaning process.
I have tried setting the DataContext from code-behind instead and it works gracefully :D.
I guess from now on i will ever setting the data context from code-behind and for intellisense purpose in XAML editor i set
d:DataContext
to "{d:DesignInstance Type=local:MainViewModel}"
This is roughly the approach I use before adding DI/IoC support. Though I use either
Stylet
or CommunityToolkit.Mvvm
as an MVVM framework. Saves a lot of time.✅ This post has been marked as answered!