programming
.NET
C#
namespace error
debugging

The name ... does not exist in the namespace clr-namespace ...

Codemia Knowledge Hub ·

Introduction

The XAML error The name "X" does not exist in the namespace "clr-namespace:Y" occurs in WPF, UWP, MAUI, and Xamarin applications when the XAML parser cannot find a type referenced in a clr-namespace declaration. This is a build-time error that prevents the designer from rendering and the application from compiling. The root cause is usually a mismatch between the namespace declaration in XAML and the actual C# namespace/class.

The Error

In XAML, you reference C# types via clr-namespace:

xml
1<Window xmlns:local="clr-namespace:MyApp.Controls"
2        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
3    <local:MyCustomControl />  <!-- Error: "MyCustomControl" does not exist -->
4</Window>

Common Causes and Fixes

1. Class Is Not Public

The referenced class must be public, not internal (the default for classes without an access modifier):

csharp
1// BAD: internal by default — not visible to XAML
2class MyCustomControl : UserControl { }
3
4// GOOD: explicitly public
5public class MyCustomControl : UserControl { }

2. Namespace Mismatch

The clr-namespace in XAML must exactly match the C# namespace:

csharp
1// C# namespace
2namespace MyApp.UI.Controls
3{
4    public class MyCustomControl : UserControl { }
5}
xml
1<!-- WRONG: namespace doesn't match -->
2<Window xmlns:local="clr-namespace:MyApp.Controls">
3
4<!-- CORRECT: exact match -->
5<Window xmlns:local="clr-namespace:MyApp.UI.Controls">

3. Project Not Built

The XAML designer resolves types from the compiled assembly. If you just added a new class, you must build the project before the XAML designer can find it:

 
Build > Build Solution (Ctrl+Shift+B)

4. Different Assembly

If the class is in a different project/assembly, you must specify the assembly name:

xml
1<!-- Class in the same project -->
2<Window xmlns:local="clr-namespace:MyApp.Controls">
3
4<!-- Class in a different assembly -->
5<Window xmlns:controls="clr-namespace:SharedLib.Controls;assembly=SharedLib">
6    <controls:MyCustomControl />
7</Window>

The assembly parameter must match the assembly name (not the project name or namespace):

bash
# Find the assembly name
# In the .csproj file:
<AssemblyName>SharedLib</AssemblyName>

5. Spaces in the clr-namespace Declaration

The clr-namespace URI must not contain spaces:

xml
1<!-- WRONG: space after semicolon -->
2<Window xmlns:local="clr-namespace:MyApp.Controls; assembly=MyApp">
3
4<!-- CORRECT: no spaces -->
5<Window xmlns:local="clr-namespace:MyApp.Controls;assembly=MyApp">

6. Target Framework Mismatch

If the referenced assembly targets a different framework, types may not resolve:

xml
<!-- WPF project referencing a .NET Standard library -->
<Window xmlns:lib="clr-namespace:MyLib.Controls;assembly=MyLib">

Ensure both projects target compatible frameworks (e.g., both .NET 8, or .NET Standard 2.0 for the library).

7. Conditional Compilation

Types inside #if directives may not be available in all build configurations:

csharp
1#if DEBUG
2public class DebugPanel : UserControl { }
3#endif
4// Not available in Release build!

Step-by-Step Debugging

Step 1: Verify the Class Exists and Is Public

csharp
1// Check:
2// 1. The class exists
3// 2. It is public
4// 3. The namespace matches exactly
5namespace MyApp.Controls
6{
7    public class MyCustomControl : UserControl
8    {
9        public MyCustomControl() { }
10    }
11}

Step 2: Build the Project

 
Build > Clean Solution
Build > Build Solution

Step 3: Check XAML Namespace Declaration

xml
1<!-- Verify no typos, no spaces, correct assembly name -->
2xmlns:local="clr-namespace:MyApp.Controls"
3<!-- or for external assemblies -->
4xmlns:ext="clr-namespace:ExternalLib.Controls;assembly=ExternalLib"

Step 4: Restart Visual Studio

The XAML designer cache can become stale. Close and reopen Visual Studio, or:

  • Delete the .vs folder
  • Delete bin and obj directories
  • Rebuild
bash
1# Clean cached data
2rm -rf .vs/ bin/ obj/
3dotnet clean
4dotnet build

WPF vs MAUI vs Xamarin Differences

PlatformNamespace FormatNotes
WPFclr-namespace:Ns;assembly=AsmStandard format
MAUIclr-namespace:Ns or xmlns:local="..."Also supports x:DataType
Xamarin.Formsclr-namespace:Ns;assembly=AsmAssembly required for external
UWPusing:NsDifferent syntax entirely

UWP/WinUI Syntax

UWP uses using: instead of clr-namespace::

xml
1<!-- UWP/WinUI -->
2<Page xmlns:local="using:MyApp.Controls">
3    <local:MyCustomControl />
4</Page>

Common Pitfalls

  • Designer vs runtime: Sometimes the designer shows the error but the project compiles and runs fine. This is a designer cache issue — rebuild and restart Visual Studio.
  • Partial classes: If your control uses a partial class (e.g., code-behind for XAML), ensure both parts are in the same namespace.
  • NuGet package classes: For types from NuGet packages, use the package's documented namespace and assembly name, not what you think they might be.
  • Refactoring namespace changes: If you rename a namespace in C#, XAML files are not automatically updated. Use a global find-and-replace to update all clr-namespace: references.
  • Generic types: Generic types like MyControl<T> cannot be directly used in XAML. Create a non-generic derived class instead.

Summary

  • Verify the class is public and the C# namespace exactly matches the clr-namespace in XAML
  • For types in other assemblies, add ;assembly=AssemblyName (no spaces)
  • Build the project after adding new classes — the designer needs a compiled assembly
  • Clean and rebuild if the error persists (rm -rf bin/ obj/ .vs/)
  • UWP uses using:Namespace instead of clr-namespace:Namespace

Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Data Structures & Algorithms practice on Codemia

Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.

Practice algorithms