命名空间中不存在新的 WPF 自定义控件库名称
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12569558/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
New WPF Custom Control Library Name does not exist in Namespace
提问by user654628
I am new to WPF and I am trying to resolve an error. I am trying to build a custom control library where I can make my own control objects. When I go to File > New Project > WPF Custom Control Library > [Enter name] > Save, then instant error:
我是 WPF 的新手,我正在尝试解决错误。我正在尝试构建一个自定义控件库,我可以在其中创建自己的控件对象。当我转到文件 > 新建项目 > WPF 自定义控件库 > [输入名称] > 保存时,立即出错:
The name "CustomControl1" does not exist in the namespace "clr-namespace:ProjectName"
I did not edit any code but immediately an error. For reference, the error is inside Generic.xaml.
我没有编辑任何代码,但立即出错。作为参考,错误在 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>
I am using Visual Studio 12 and .NET 4. Any ideas?
我正在使用 Visual Studio 12 和 .NET 4。有什么想法吗?
采纳答案by James McNellis
This is an IntelliSense error, not a build error, so it should not affect building the project. To resolve this error, either
这是 IntelliSense 错误,而不是构建错误,因此它不会影响构建项目。要解决此错误,要么
- build the project, or
- edit the document (e.g. by removing a
>from a tag, then adding it back).
- 构建项目,或
- 编辑文档(例如,
>从标签中删除 a ,然后将其添加回来)。
When you open the document, the XAML designer loads in the background to provide IntelliSense information. It loads information for unbuilt types (i.e., types defined in the current Solution but which have not yet been built into an assembly) asynchronously, and often this process completes after the designer has completed the initial parse of the document.
当您打开文档时,XAML 设计器会在后台加载以提供 IntelliSense 信息。它异步加载未构建类型(即在当前解决方案中定义但尚未构建到程序集中的类型)的信息,并且该过程通常在设计者完成文档的初始解析之后完成。
Building the project will cause the unbuilt types to be built (thus resolving the issue), and making a material edit to the document will cause the designer to reparse the document with the newly available type information (ideally, the document should be reparsed when the unbuilt type information becomes available).
构建项目将导致构建未构建的类型(从而解决问题),对文档进行材质编辑将导致设计人员使用新可用的类型信息重新解析文档(理想情况下,应重新解析文档)未构建的类型信息变得可用)。
回答by dba
According to the answer of James McNellis, in my case I had to comment out the XAML-section causing the error (since the error did not allow to rebuild), then CLOSE the file itself, so its not open in VS, then I could make a successful build. Then I uncommented the XAML-section and VS was able to locate the local classes... In case, someone would stumble on this. Visual Studio 2012 Express for Windows Desktop... BR,
根据 James McNellis 的回答,在我的情况下,我不得不注释掉导致错误的 XAML 部分(因为错误不允许重建),然后关闭文件本身,因此它不能在 VS 中打开,然后我可以构建成功。然后我取消了 XAML 部分的注释,并且 VS 能够找到本地类......以防万一,有人会偶然发现这一点。适用于 Windows 桌面的 Visual Studio 2012 Express... BR,
Daniel
丹尼尔
回答by Stephen
Assuming your assembly is called ProjectName, and your target namespaceis also called ProjectName
假设您的程序集称为ProjectName,并且您的目标命名空间也称为ProjectName
You have to change the following:
您必须更改以下内容:
xmlns:local="clr-namespace:ProjectName">
to
到
xmlns:local="clr-namespace:ProjectName;assembly=ProjectName">
回答by Bizhan
This can also happens if you don't write the default style for CustomControl in generic.xaml. It generates something like this in generic.xaml:
如果您不在generic.xaml. 它在 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>
you can find this piece of code and change or remove it.
您可以找到这段代码并更改或删除它。
回答by LueTm
Rebuilding and cleaning several times fixed the issue for me.
多次重建和清洁为我解决了这个问题。
回答by Big Daddy
Make sure that CustomControl1 doesn't have any errors in it and check your namespace.
确保 CustomControl1 中没有任何错误并检查您的命名空间。

