【问题标题】:Error in a .g.cs file?.g.cs 文件中的错误?
【发布时间】:2011-10-14 08:16:57
【问题描述】:
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

而麻烦的是:

using BreadcrumbLib;

昨天我从我的解决方案中删除了那个项目 (BreadcrumbLib),主项目运行良好。
今天它不起作用。

对此我能做些什么?
哪些被遗弃的信息使 Visual Studio 或任何构建 .g.cs 文件的程序将引用放在那里?

【问题讨论】:

    标签: c# build


    【解决方案1】:

    尝试清洁。如果这不起作用,请删除解决方案中的 bin obj 目录,然后进行完全重建。确保没有任何 XAML 文件引用该组件(这是生成 .g.cs 文件的内容)。

    【讨论】:

    • @Jeff 所说的 +1(我在打字时接了一个电话!)
    • 这行得通...但是为什么我的项目昨天(在另一台机器上)我删除了 BreadcrumbLib 之后构建?
    • 确实是 XAML 中的引用。
    • 对任何有类似问题的人的说明:我必须从文件“b”中删除引用才能从文件“a”中删除错误。所以继续寻找(找到所有帮助)
    • 为了澄清这一点,您是否同时删除了 bin 和 obj 文件夹?我想答案是肯定的。
    【解决方案2】:

    我的项目中的 .g.cs 文件通常是从 XAML 生成的。

    检查是否在任何 XAML 文件的 xmlns 引用中都没有引用 BreadcrumbLib。

    【讨论】:

    • 这对我有用,谢谢。无论如何,xaml 编译器应该警告 xaml 的错误而不是 g.cs
    【解决方案3】:

    这也发生在我身上,因为我将一些 Xaml 内容移动到了不同文件夹中的子 userControl,并且 Resharper 为我移动了 Xmlns 引用。

    但是,ReSharper 没有使用之前使用的 Xmlns 引用(具体而言:xmlns:ms="clr-namespace:Mindscape.WpfElements.Charting;assembly=Mindscape.WpfElements"),而是将其替换为以下内容:

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

    我用旧的正确引用替换了不正确的引用。这为我解决了问题。

    【讨论】:

      【解决方案4】:

      如果像我这样的任何人遇到此问题,我的问题的原因是我的“GlobalStyles.xaml”资源字典引用了已删除的命名空间,并且由于某种原因它显示为 MainWindow.g 中的包含项.cs 文件。删除资源字典文件中的引用修复了它。

      【讨论】:

        【解决方案5】:

        这只是发生在我身上。我的问题是我的x:Name 中有空格。一旦我删除了空间并清理了解决方案,它就可以正常工作。

        不正确

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

        正确

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

        【讨论】:

          【解决方案6】:

          刚刚收到此错误消息。

          这里的答案都没有解决我的问题,但我查看了我的警告消息,发现其中一个项目是在 .NET 4.7 中构建的,另一个是在 4.5 中构建的。我将所有项目都放在同一个版本上,构建,现在我很高兴!

          【讨论】:

            【解决方案7】:

            我刚遇到同样的问题,

            我在 nuget 中创建了一个自定义控件,而生成的 xmal 类只是不喜欢所涉及的命名空间。结果控件有一个命名空间xxx.yyy.zzz.Name,而我的项目中有一个命名空间eee.fff.yyy...(注意yyy),它完全被弄糊涂了,唯一的办法就是重构父应用程序中的命名空间。

            【讨论】:

              【解决方案8】:

              我遇到了类似的问题,但我的问题是我从 WPF UI 库中删除了几个文件夹/命名空间,而忘记从库的 AssemblyInfo.cs 文件中删除相应的 XmlnsDefinition 条目。我让它们都指向“http://schemas.foo.com/ui”,以便更容易在 .xaml 文件中引用,如下所示:

              [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")]
              

              在 AssemblyInfo.cs 中,并且:

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

              在 MainWindow.xaml 中。

              我删除了 ViewModel 和命令,但忘记删除 [assembly:] 属性,因此在我重建时它正在寻找它们。删除了违规行,等等。

              我想我会分享给其他做过类似事情但可能会摸不着头脑的人。

              【讨论】:

                【解决方案9】:

                我的情况有点不同。我有一个 WPF 项目,我创建了一个这样的自定义面板:

                using System;
                using System.Windows;
                using System.Windows.Controls;
                
                namespace MegaMenu
                {
                    public class MegaMenu : Panel
                    {
                

                我的 .xaml 文件如下所示:

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

                这给了我g.cs 文件中的错误等等。因此,当命名空间名称与类名称相同时,似乎会发生此问题。我必须将它们更改为导致错误消失的下方。

                .cs 文件
                using System;
                using System.Windows;
                using System.Windows.Controls;
                
                namespace MegaMenu
                {
                    public class MegaMenuPanel : Panel
                    {
                
                xml 文件
                <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>
                

                【讨论】:

                  【解决方案10】:

                  就我而言,我曾尝试使用 x:Name 命名 xaml 控件,但忘记了,并将其保留为 x:Name=""。编译器一直在抛出错误。提供正确的名称或删除该空字符串为我解决了错误。

                  【讨论】:

                    猜你喜欢
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    • 2013-06-07
                    • 1970-01-01
                    • 2022-12-29
                    • 1970-01-01
                    • 1970-01-01
                    • 2014-07-17
                    相关资源
                    最近更新 更多