New WPF Custom Control Library Name does not exist in Namespace
Asked Answered
C

7

13

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:

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.

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

Chlorine answered 24/9, 2012 at 16:58 Comment(2)
What is the fully qualified name of your CustomControl1 class?Constitutionalism
I didn't change it. I made a new project (did not edit anything), tried to build and it failed to build because of that error.Chlorine
I
16

This is an IntelliSense error, not a build error, so it should not affect building the project. To resolve this error, either

  • build the project, or
  • edit the document (e.g. by removing a > from a tag, then adding it back).

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.

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

Irreformable answered 24/9, 2012 at 17:9 Comment(6)
Nearly 3 years later, this bug still exists. Unbelievable!Gondi
5 years later, this bug exists, and this answer doesn't refresh intellisense :(Interpolate
7 years later and guess what?Saltatorial
9 years later....Justus
10 years later......Theroid
12 years later.....Null
B
4

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,

Daniel

Benevento answered 3/8, 2015 at 10:15 Comment(0)
L
2

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:

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

Lucia answered 3/5, 2014 at 10:40 Comment(0)
A
1

Assuming your assembly is called ProjectName, and your target namespace is also called ProjectName

You have to change the following:

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

to

xmlns:local="clr-namespace:ProjectName;assembly=ProjectName">
Attic answered 19/9, 2013 at 1:14 Comment(0)
B
0

Make sure that CustomControl1 doesn't have any errors in it and check your namespace.

Belita answered 24/9, 2012 at 17:10 Comment(2)
How can it have any errors if it was the default template from Visual Studio?Chlorine
Okay, so you didn't make any modifications - code, namespace, etc. and it gives you an error. What about James McNellis' answer, did it help?Belita
C
0

Rebuilding and cleaning several times fixed the issue for me.

Crissman answered 9/7, 2015 at 7:45 Comment(0)
S
0

Modify the derived class in derivedclass.cs to Abstract:

public abstract class CustomControl : Control

And then use it in the .xaml files

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

//also see this topic: Inheriting from a UserControl in WPF

Saltatorial answered 15/6, 2021 at 0:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.