【问题标题】:New WPF Custom Control Library Name does not exist in Namespace命名空间中不存在新的 WPF 自定义控件库名称
【发布时间】:2012-09-24 16:58:04
【问题描述】:

我是 WPF 新手,我正在尝试解决一个错误。我正在尝试构建一个自定义控件库,我可以在其中制作自己的控件对象。当我转到文件 > 新项目 > WPF 自定义控件库 > [输入名称] > 保存时,立即出现错误:

The name "CustomControl1" does not exist in the namespace "clr-namespace:ProjectName"

我没有编辑任何代码,但立即出现错误。作为参考,错误在 Generic.xaml 中。

<ResourceDictionary
       xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
       xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
       xmlns:local="clr-namespace:ProjectName">
       <Style TargetType="{x:Type local:CustomControl1}">  //<--Fails here
           <Setter Property="Template">
               <Setter.Value>
                   <ControlTemplate TargetType="{x:Type local:CustomControl1}">  // Fails here as well
                       <Border Background="{TemplateBinding Background}"
                               BorderBrush="{TemplateBinding BorderBrush}"
                               BorderThickness="{TemplateBinding BorderThickness}">

                       </Border>
                   </ControlTemplate>
               </Setter.Value>
            </Setter>
        </Style>
    </ResourceDictionary>

我正在使用 Visual Studio 12 和 .NET 4。有什么想法吗?

【问题讨论】:

  • 你的CustomControl1 类的fully qualified 名称是什么?
  • 我没有改变它。我创建了一个新项目(没有编辑任何内容),尝试构建,但由于该错误而无法构建。

标签: wpf visual-studio intellisense visual-studio-2012 xaml-designer


【解决方案1】:

这是一个 IntelliSense 错误,而不是构建错误,因此它不会影响构建项目。要解决此错误,

  • 构建项目,或
  • 编辑文档(例如,从标签中删除&gt;,然后将其添加回来)。

当您打开文档时,XAML 设计器会在后台加载以提供 IntelliSense 信息。它异步加载未构建类型(即在当前解决方案中定义但尚未构建到程序集中的类型)的信息,并且此过程通常在设计者完成文档的初始解析后完成。

构建项目将导致构建未构建的类型(从而解决问题),对文档进行材料编辑将导致设计人员使用新可用的类型信息重新解析文档(理想情况下,文档应该是当未构建的类型信息可用时重新解析)。

【讨论】:

  • 将近 3 年后,这个 bug 仍然存在。难以置信!
  • 5 年后,这个 bug 存在,这个答案不会刷新智能感知 :(
  • 7 年后你猜怎么着?
  • 9 年后....
【解决方案2】:

根据 James McNellis 的回答,在我的情况下,我必须注释掉导致错误的 XAML 部分(因为错误不允许重建),然后关闭文件本身,所以它不能在 VS 中打开,然后我可以成功构建。然后我取消注释 XAML 部分,VS 能够找到本地类...... 以防万一,有人会偶然发现这一点。 适用于 Windows 桌面的 Visual Studio 2012 Express... BR,

丹尼尔

【讨论】:

    【解决方案3】:

    如果您没有在 generic.xaml 中编写 CustomControl 的默认样式,也会发生这种情况。它会在 generic.xaml 中生成类似的内容:

    <Style TargetType="{x:Type local:MyCustomControl}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type local:MyCustomControl}">
                    <Border Background="{TemplateBinding Background}"
                            BorderBrush="{TemplateBinding BorderBrush}"
                            BorderThickness="{TemplateBinding BorderThickness}">
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    

    您可以找到这段代码并更改或删除它。

    【讨论】:

      【解决方案4】:

      假设您的程序集称为 ProjectName,而您的目标 namespace 也称为 ProjectName

      您必须更改以下内容:

      xmlns:local="clr-namespace:ProjectName">
      

      xmlns:local="clr-namespace:ProjectName;assembly=ProjectName">
      

      【讨论】:

        【解决方案5】:

        确保 CustomControl1 没有任何错误并检查您的命名空间。

        【讨论】:

        • 如果它是 Visual Studio 的默认模板,怎么会有任何错误?
        • 好的,所以您没有进行任何修改 - 代码、命名空间等,它会给您一个错误。 James McNellis 的回答怎么样,有帮助吗?
        【解决方案6】:

        多次重建和清理为我解决了这个问题。

        【讨论】:

          【解决方案7】:

          将derivedclass.cs中的派生类修改为Abstract:

          public abstract class CustomControl : Control
          

          然后在 .xaml 文件中使用它

          <local:CustomControl>
          ...
          </local:CustomControl>
          

          //也看这个话题:Inheriting from a UserControl in WPF

          【讨论】:

            猜你喜欢
            • 2016-09-22
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2016-03-08
            • 1970-01-01
            • 2014-10-19
            • 2019-06-26
            • 1970-01-01
            相关资源
            最近更新 更多