63
Error   1   The type or namespace name 'BreadcrumbLib' could not be found (are you missing a using directive or an assembly reference?) YOU_CAN'T_KNOW_THE_PATH_OR_NAME.g.cs    16  7   someWPFapp

And the troublesome line is:

using BreadcrumbLib;

Yesterday I have removed that project (BreadcrumbLib) from my solution and the main project worked fine.
Today it doesn't work.

What can I do about this?
What piece of forsaken information makes Visual Studio or whatever program builds the .g.cs files put that reference there?

15 Answers 15

102

Try doing a clean. If that doesn't work, delete the bin obj directories in your solution, then do a full rebuild. Make sure none of your XAML files are referencing that component (which is what's generating the .g.cs file).

8
  • 1
    +1 for what @Jeff says (I answered a phone call while typing mine!)
    – Stuart
    Commented Jul 27, 2011 at 14:44
  • This worked... But why did my project build yesterday (on another machine) after I removed BreadcrumbLib?
    – Vercas
    Commented Jul 27, 2011 at 14:50
  • 9
    It was indeed a reference in the XAML.
    – Vercas
    Commented Jul 27, 2011 at 19:32
  • 2
    A note for anyone with similar issues: I had to remove a reference from file 'b' in order to remove the error from file 'a'. So just keep looking (find all helps)
    – RyanS
    Commented Feb 26, 2014 at 16:00
  • 1
    Just to clarify this, do you delete both the bin and the obj folders? I presume the answer is yes.
    – Rod
    Commented Apr 20, 2018 at 16:27
33

The .g.cs files in my projects are typically generated from XAML.

Check that the BreadcrumbLib isn't referenced in the xmlns references in any of your XAML files.

1
  • 4
    This worked for me, thanks. Anyway the xaml compiler should warn about an error at the xaml not at the g.cs Commented Oct 27, 2012 at 8:29
6

This happened to me too because I moved some of my Xaml content to a sub userControl in a different folder, and Resharper moved the Xmlns references for me.

However, instead of using the Xmlns reference that was used before (specifically: xmlns:ms="clr-namespace:Mindscape.WpfElements.Charting;assembly=Mindscape.WpfElements"), ReSharper replaced it with the following:

xmlns:ms="http://namespaces.mindscape.co.nz/wpf" 

I replaced the incorrect reference with the old correct one. That solved the problem for me.

5

I had this issue as well. For me the problem was that a rename didn't go through all the way in the razor files, so the g.cs files kept getting generated with the old name.

I solved it by checking the related .razor file for errors and fixing them manually.

1
  • same here. I did a rename from IdentityUser to ApplicationUser. The pages that were failing with g.cs error was due to the using namespace of my ApplicationUser needed to be added.
    – tvb108108
    Commented Jun 27, 2022 at 0:19
4

Just got this error message.

None of the answer here solved my issue, but I looked at my warnings message and saw that one of the project was build in .NET 4.7 and the other in 4.5. I put all project on the same version, build and now I'am happy!

1
  • I had NuGet package version conflicts between some projects, including test projects. Commented Sep 6, 2023 at 7:42
3

In case anyone, like myself, encounters this issue, the cause for my issue was that my "GlobalStyles.xaml" resource dictionary was referencing the removed namesapce, and for some reason it showed up as an include in the MainWindow.g.cs file. Removing the reference in the resource dictionary file fixed it.

2

This just happened to me. My issue was that I had spaces in my x:Name. Once I removed the spaces and cleaned the solution it worked fine.

Incorrect

<button Text="Check In" x:Name="Check In">

Correct

<button Text="Check In" x:Name="CheckIn">
2

Had the same error. The root cause was that I had renamed several of the viewModels and as it turned out my find & replace was not complete.

I resolved the issue by looking for the offending view model name in the error message, did a global search and replace in my views (not the .g.cs file). and the issue went away.

1

I just experienced the same issue,

I had a custom control i created in a nuget, and the generated xmal class just didn't like the namespace involved. It turns the control had a namespace of xxx.yyy.zzz.Name and my project had a namespace in it, eee.fff.yyy... (notice the yyy) it just got completely confused and the only recourse was to refactor the namespaces in the parent app.

1

Ran into g.cs issues and deleting /bin and /obj did not help. If you renamed namespaces or classes, manually open each .cshtml file (double clicking the error will not do this in VS 2022 for some reason) and make sure the .cshtml has no syntax errors. In my case, I was pointing @model to the previous namespace.. missed these when using the replace tool and my various find/replace lookups.

2
0

I had a similar problem, but my issue was that I had removed a couple of folders/namespaces from a WPF UI library, and forgotten to remove the corresponding XmlnsDefinition entries from the library's AssemblyInfo.cs file. I had had them all pointing to "http://schemas.foo.com/ui" to make it easier to reference in .xaml files, like so:

[assembly: XmlnsPrefix("http://schemas.foo.com/ui", "ui")]
[assembly: XmlnsDefinition("http://schemas.foo.com/ui", "Foo.UI.Common.Behaviors")]
[assembly: XmlnsDefinition("http://schemas.foo.com/ui", "Foo.UI.Common.Converters")]
[assembly: XmlnsDefinition("http://schemas.foo.com/ui", "Foo.UI.Common.Commands")]
[assembly: XmlnsDefinition("http://schemas.foo.com/ui", "Foo.UI.Common.ViewModels")]
[assembly: XmlnsDefinition("http://schemas.foo.com/ui", "Foo.UI.Common.UserControls")]
[assembly: XmlnsDefinition("http://schemas.foo.com/ui", "Foo.UI.Common.Extensions")]

in AssemblyInfo.cs, and:

xmlns:ui="http://schemas.foo.com/foo/ui/"

in MainWindow.xaml.

I removed ViewModels and Commands, but forgot to remove the [assembly:] attributes, so it was looking for them when I rebuilt. Removed the offending lines, et voila.

Figured I'd share for anyone else who did stuff like this and might be scratching their heads.

0

My case is a little different. I have a WPF project and I've created a custom Panel like this:

using System;
using System.Windows;
using System.Windows.Controls;

namespace MegaMenu
{
    public class MegaMenu : Panel
    {

My .xaml file looks like this:

<Window x:Class="MegaMenu.MainWindow"
        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:local="clr-namespace:MegaMenu"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800"

        >
    <local:MegaMenu>
        <TextBox Width="50" Height="20" Text="First"></TextBox>
        <TextBox Width="50" Height="20" Text="Second"></TextBox>
        <TextBox Width="50" Height="20" Text="Third"></TextBox>
        <TextBox Width="50" Height="20" Text="Fourth"></TextBox>
        <TextBox Width="50" Height="20" Text="Fifth"></TextBox>
        <TextBox Width="50" Height="20" Text="Sixth"></TextBox>
        <TextBox Width="50" Height="20" Text="Seventh"></TextBox>
    </local:MegaMenu>
</Window>

This give's me the error in g.cs file blah blah. So it seems this issue happens when the namespace name is the same as the class name. I have to change them to below which caused the error gone.

.cs file
using System;
using System.Windows;
using System.Windows.Controls;

namespace MegaMenu
{
    public class MegaMenuPanel : Panel
    {
xaml file
<Window x:Class="MegaMenu.MainWindow"
        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:local="clr-namespace:MegaMenu"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800"

        >
    <local:MegaMenuPanel>
        <TextBox Width="50" Height="20" Text="First"></TextBox>
        <TextBox Width="50" Height="20" Text="Second"></TextBox>
        <TextBox Width="50" Height="20" Text="Third"></TextBox>
        <TextBox Width="50" Height="20" Text="Fourth"></TextBox>
        <TextBox Width="50" Height="20" Text="Fifth"></TextBox>
        <TextBox Width="50" Height="20" Text="Sixth"></TextBox>
        <TextBox Width="50" Height="20" Text="Seventh"></TextBox>
    </local:MegaMenuPanel>
</Window>
0

In my case , I was tried to name a xaml control using x:Name , and forgot , and kept it as x:Name="" . The compiler was keep on throwing error. Give proper name or remove that empty string solves the error for me.

0

I was not using WPF but got this error because I forgot to change the namespace in the @inject part of my razor view.

0

I got this error down grading a .NET 6 project to .NET 4.8.

By default .NET 6 added into my .csproj <ImplicitUsings>enable</ImplicitUsings> which if I removed forced me add the using statements manually which resolved my issue of a phantom using thinking it was needed when it wasn't and couldn't be found.

Not the answer you're looking for? Browse other questions tagged or ask your own question.