5
votes

To illustrate the issue, I have created a simple sample project where I am getting compilation errors on the XAML definition.

Here is my class definition:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MVVMTest.Foo
{
    public class FooTestClass
    {
        public String Name { get; set; }

        public String Key { get; set; }
    }
}

It should be noted that FooTestClass resides in the Foo subfolder (of the MVVMTest folder)

Here is my XAML:

<Window x:Class="MVVMTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:MVVMTest"
        xmlns:mView="clr-namespace:MVVMTest.Foo;assembly=MVVMTest"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">

    <Window.Resources>
        <mView:FooTestClass x:Key="ddd" />           
    </Window.Resources>
    <Grid>
    </Grid>
</Window>

I'm getting the following error message

The tag 'FooTestClass' does not exist in XML namespace 'clr-namespace:MVVMTest.Foo;assembly=MVVMTest'. Line 12 Position 10.
MVVMTest C:\Dev Projects\MVVMTest\MVVMTest\MainWindow.xaml

Is there an issue in VS?

2
This SO has an answer suggesting removing the assembly value and leaving it empty. " xmlns:ZZZ="clr-namespace:YYY;assembly=" ".Ethilium
SO - Thanks, that would be it. I'm still seeing pretty flakey behaviour with namespaces in XAML, but it seems to work now.JohnB

2 Answers

3
votes

You should not specify the name of the assembly if the type is defined in the same assembly as the window:

xmlns:mView="clr-namespace:MVVMTest.Foo"

An exception from this rule is when you load some XAML markup dynamically using the XamlReader.Parse method. Then you must specify the assembly name even if the type resides in the same assembly.

3
votes

I've found yet another reason why this error may occur, so I'll leave it here for future readers.

If the namespace solution doesn't work, make sure your project and your DLL are both targeting the same framework versions.

Menu Project -> Properties

enter image description here