Mysterious XAML “The name "X" does not exist in the namespace” error won't go away

Will Pittenger 281 Reputation points
2024-07-28T01:51:58.1333333+00:00
I'm working on a XAML/C# project.  I have a custom abstract class derived from `System.Windows.Controls.UserControl`: `PageBase`.  Visual Studio 2022 keeps telling me it can't find `PageBase` in the namespace.

The error:


```sql
Severity	Code	Description	Project	File	Line	Suppression State	Details
Error	XDG0008	The name "PageBase" does not exist in the namespace "clr-namespace:SupremeCourtWPF.StartWizard".	SupremeCourtWPF	B:\Contents\Dr. Leff\SupremeCourtWPF\SupremeCourtWPF\SupremeCourtWPF\StartWizard\TermsPage.xaml	1

Here's my class's declaration:

namespace SupremeCourtWPF.StartWizard
{
	public abstract class PageBase : System.Windows.Controls.UserControl, System.ComponentModel.INotifyPropertyChanged

Here's where it complains in XAML:

local:PageBase
	x:Class="SupremeCourtWPF.StartWizard.TermsPage"
	xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
	xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
	xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
	xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
	xmlns:local="clr-namespace:SupremeCourtWPF.StartWizard"

Any ideas what's going on? I tried deleting the solution's .vs folder and deleting the obj and bin folders. Those didn't help. I also tried adding the assembly name to the xmlns declaration. That didn't help either.


.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,651 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,655 questions
XAML
XAML
A language based on Extensible Markup Language (XML) that enables developers to specify a hierarchy of objects with a set of properties and logic.
791 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Michael Taylor 51,426 Reputation points
    2024-07-28T02:24:41.22+00:00

    In your XAML you are attempting to create an instance of PageBase by using the local:PageBase syntax. But it is an abstract class and cannot be created, hence the error.

    To create a custom user control you would normally create the type without abstraction and then define it as a UserControl in its corresponding XAML with the type as the x:Class name. Hence it is a UserControl but the underlying type is whatever x:Class defines it as.

    To consume the UC you would simply include it in another window/control. At this point it needs to be a creatable type since you cannot create abstract instances.

    I think you are mixing up your syntax in your XAML. You're trying to declare the UC and consume it at the same time which won't work. Refer to the following tutorial here on how to properly create a user control. Fixing the XAML and changing the UC to not be abstract (or not try to use it directly but instead deriving another class from it, like your TermsPage would resolve the issue.

    0 comments No comments