C# – The name “XYZ” does not exist in the namespace “clr-namespace:ABC”

cwpfxaml

I was working on creating some markup extensions and started to get very weird VS behaviours. I have extracted and pinpointed the issue in the separate solution. Problem is that VS can't create a CLR object in XAML.

Here it is:

View:

<Window x:Class="WpfApplication4.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:wpfApplication4="clr-namespace:WpfApplication4">
    <Window.Resources>
        <wpfApplication4:Dog x:Key="doggy" />
    </Window.Resources>
    <Grid />
</Window>

Code behind:

using System.Windows;

namespace WpfApplication4
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
    }
}

Dog class:

namespace WpfApplication4
{
    public class Dog
    {

    }
}

App.Xaml (no code in App.Xaml.cs):

<Application x:Class="WpfApplication4.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             StartupUri="MainWindow.xaml">
    <Application.Resources>

    </Application.Resources>
</Application>

Project settings

Exception I am getting:

Error   1   The name "Dog" does not exist in the namespace "clr-namespace:WpfApplication4". \\hopr1\folders$\vxk\Documents\Visual Studio 2012\Projects\WpfApplication4\MainWindow.xaml  6   9   WpfApplication4

I am able to run solution, but designer fails with "Invalid Markup" error
Any ideas?

Edit

I am running VS 2012 Update 2
The same solution work in VS 2012 Update 1

Best Answer

For anyone coming across this now, BEFORE YOU DO ANYTHING ELSE... if you're certain your classes/namespaces are correct and rebuilding has not solved your problem:

Try restarting Visual Studio

That's it!

This appears to be a bug with Visual Studio 2012 (also appears to affect all other versions that support XAML development)


Update: If restarting Visual Studio doesn't work, reboot the entire PC.

Update: As mentioned in comments by @Dunk, if restarting Visual Studio doesn't work, try deleting the .suo file