Error in a .g.cs file?

C#Build

C# Problem Overview


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?

C# Solutions


Solution 1 - C#

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).

Solution 2 - C#

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.

Solution 3 - C#

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.

Solution 4 - C#

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.

Solution 5 - C#

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">

Solution 6 - C#

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!

Solution 7 - C#

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.

Solution 8 - C#

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.

Solution 9 - C#

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.

Solution 10 - C#

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>

Solution 11 - C#

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionVercasView Question on Stackoverflow
Solution 1 - C#JeffView Answer on Stackoverflow
Solution 2 - C#StuartView Answer on Stackoverflow
Solution 3 - C#willemView Answer on Stackoverflow
Solution 4 - C#CodeMonkeyView Answer on Stackoverflow
Solution 5 - C#Marcello B.View Answer on Stackoverflow
Solution 6 - C#RaphaelView Answer on Stackoverflow
Solution 7 - C#TheGeneralView Answer on Stackoverflow
Solution 8 - C#MartijnView Answer on Stackoverflow
Solution 9 - C#Mike LouxView Answer on Stackoverflow
Solution 10 - C#Just a learnerView Answer on Stackoverflow
Solution 11 - C#NoorulView Answer on Stackoverflow