I have an issue that is driving me nuts. When I try to build my VB.NET WPF project in Visual Studio, I get an error "Type 'MyProject.MyUC' is not defined". This error appeared after I made some changes to other files from this solution (completely unrelated to that UC, it's backend code, or the window that uses that UC. Entirely separate code). I am using GIT, and I'm comparing old branch with a new branch, so I know this for a fact.
The window that uses the control is defined like this:
<Window x:Class="MyWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:uc="clr-namespace:MyProject"
xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit"
mc:Ignorable="d"
Title="MyWindow" Height="350" Width="650">
<Grid>
<uc:MyUC x:Name="MyUC"></uc:MyUC>
</Grid>
</Window>
The UC is defined like this:
<UserControl x:Class="MyUC"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:FLOATSOFT"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="600">
<Grid>
</Grid>
</UserControl>
And it's backed VB code:
Public Class MyUC
End Class
(No specific namespace declarations)
I have removed all functional code to show the structure. But even if I actually trim down my code to what I quoted, it doesn't compile.
I don't get it. MyUC
is definitely in the MyProject
namespace. If I hover my mouse over the class name in VB code, the tooltip says: "Class MyProject.MyUC", confirming it is definitely in the right namespace.
Why could this be happening? I still have all this that I quoted here in another GIT branch, and even though all these files match 100%, that other branch compiles, while this one gives this dreaded "Type 'MyProject.MyUC' is not defined" error.
I have tried:
Restarting Visual Studio - this used to help with such errors befoe;
Cleaning and rebuilding;
Re-creating all these files (MyWindow.XAML, MyWindow.XAML.VB, MyUC.XAML and MyUC.XAML.VB) and copying code to these new files;
Making sure my code builds if I comment out the usage of this UC and that there are no other compile errors.
Explicitly stating the namespace in the MyUC.XAML:
<UserControl x:Class="MyProject.MyUC"
And in MyUC.XAML.VB:
Namespace MyProject
Public Class MyUC
End Class
End Namespace
Doesn't help either. Same error.
- Creating a new project and copying over these 4 files. It compiles. But it would be ridiculous having to re-create the project every time this happens. Like I said, it happens often, but restarting Visual Studio used to help. Not this time.
And I am at complete loss. I have reviewed dozens of similar posts to this one, but haven't found the answer. One answer suggested that this might be due to different .NET versions of the project and referenced assemblies, but I don't have any of that.
One branch compiles, another one throws this error. Both have identical code in every file that could possibly be related to this UC. How do I find the cause?
EDIT: I forgot to mention that I also tried creating a new UC from scratch in this project branch, and if I try to use it like this MyUC, this project branch won't compile. But the other branch still compiles.
EDIT: VERY IMPORTANT: I just discovered that if I change
<Grid>
<uc:MyUC x:Name="MyUC"></uc:MyUC>
</Grid>
To
<Grid>
<uc:MyUC></uc:MyUC>
</Grid>
(Omitting x:Name="MyUC"), then suddenly code compiles and I get no error. If I set a name to this UC - any name whatsoever - it won't compile. I have absolutely no idea what is happening.
CodePudding user response:
I finally figured it out. Turns out, this issue is identical to this one. In short, I was right - nothing was changed in these 4 files, and they should have compiled. However, in a completely separate file, I incorrectly defined a namespace for one of the modules. I should have written:
Namespace MyModule
Public Module MyModule
End Class
End Namespace
But instead, I wrote:
Namespace MyProject.MyModule
Public Module MyModule
End Class
End Namespace
And although this MyModule
is absolutely unrelated to the MyUC
or MyWindow
, apparently it created a namespace MyProject.MyProject
, and that is why MyWindow wouldn't compile, as it tried to search for MyUC
in MyProject.MyProject
, presumably. Which is very weird, because the error I got did not suggest this. In fact, nothing anywhere ever suggested that I have this kind of namespace. Like I wrote in my question, everywhere I debugged, I found that MyUC
has is declared directly in MyProject.MyUC
. And every other window had the same declaration, yet they compiled.
I must admit that the error thrown by VS compiler was extremely unintuitive and the information it provided on namespaces was completely wrong for that UC, and I discovered the solution simply by accident. Even the Object Viewer showed that there is no issue with namespaces (it didn't show that I have MyProject.MyProject
).
Hope this helps if anyone encounters the same issue. And I would also love to hear any suggestions on how to make these compiler errors more straightforward to the actual issue, because in this instance it was just plain wrong.