26

I am getting error like "Type reference cannot find public type named 'Sign'" in xaml. how can i resolve it. the Sign class is in the same assembly.

<DataTemplate DataType="{x:Type local:Sign}">
    <Expander Padding="4"
              IsExpanded="{Binding RelativeSource={
                    RelativeSource Mode=FindAncestor, AncestorType={
                       x:Type ListBoxItem}}, Path=IsSelected}">
        <Expander.Header>
            <TextBlock Text="{Binding Name}" ... />
        </Expander.Header>
        <DockPanel LastChildFill="True">
            <Border DockPanel.Dock="Left" CornerRadius="16" BorderBrush="WhiteSmoke" Background="AliceBlue" BorderThickness="5" HorizontalAlignment="Center" VerticalAlignment="Center">
                <Image Source="{Binding Icon}" Width="90" Height="90" Stretch="Fill" />
            </Border>
            ...
        </DockPanel>                
    </Expander>

Tanya
  • 1,571
  • 5
  • 22
  • 33

8 Answers8

72

If the type resides in the same assembly as the XAML you are modifying, omit the assembly segment of the clr-namespace when you import the namespace.

DO

xmlns:local="clr-namespace:NamespaceContainingSignClass"

DO NOT

xmlns:local="clr-namespace:NamespaceContainingSignClass;assembly=AssemblyContainingSignClassAndXAML"
smelch
  • 2,483
  • 1
  • 18
  • 19
  • That worked for me. Can you provide an explanation for why the type reference cannot be found otherwise? – Dennis Kassel Jul 14 '17 at 09:42
  • 1
    @DennisKassel I don't know why it works that way. If I were to speculate, I would say if assembly is omitted, it searches the assembly containing the XAML, and if assembly is specified, it looks through the referenced assemblies for the specified assembly and searches through that. Since the current assembly is not in the referenced assemblies, it can't find it. Something weird like that in the current implementation. – smelch Jul 17 '17 at 02:53
  • Thank you for your comment. A few days ago I also got a confirmation from a book that you have to omit the assembly name when the namespace resides in the same assembly. – Dennis Kassel Jul 18 '17 at 16:47
  • VS automatically generates xmlns:local as the one with assembly. Should we name it something else? Do they have this documented? – Alexander Aug 07 '18 at 19:11
14

For those in my boat who weren't helped by the top 1,00 results for this error on Google....in my case it was the precise opposite of the advice from smelch: i had to ADD the assembly info on the end of my xmlns declaration. Likely because of my particular circumstances, i guess - my datatemplate was in a stand-alone resourcedictionary xaml file. Not sure. I just know it didn't work until i added the assembly info, so for those floundering out there give that a whack and see if it works. i'm not inclined to dive into why, it just did.

tntwyckoff
  • 539
  • 5
  • 17
  • I've clarified my answer. You only omit the assembly name if the class is in the same assembly as the XAML. – smelch Aug 04 '14 at 20:11
  • 4
    Having run into this problem myself, *adding* the assembly name fixed the problem too. Mine's in a ResourceDictionary xaml file in the same assembly as the class, but still needed the assembly name to be added. – Philip C Apr 23 '15 at 14:19
  • Thank you! I've also used a ResourceDictionary and wanted to load it dynamically. I just added ";assembly=..." and changed the build process to "None" instead of "Page" and it worked fine. For some reason I had to change the build process in order to compile the programm. Otherwise it would have thrown strange errors. – kaliba Feb 20 '16 at 12:36
9
  1. Check if the root tag of the xaml file has the namespace with class Sign mapped to local alias: xmlns:local="clr-namespace:NamespaceContainingSignClass"
  2. Check whether class Sign is marked public
  3. Check whether class Sign has parameterless constructor
EvAlex
  • 2,888
  • 1
  • 20
  • 24
  • 2
    Wow! Parameterless constructor! Thank you. I wonder why such a weird requirements and how did you learn about it? – Kirill Osenkov Jul 21 '13 at 06:14
  • 4
    This requirement is based on the fact that you cannot specify constructor parameters in XAML. When you type in XAML actually you mean to create Button class instance, so the constructor will be called. XAML is declarative alternative to writing markup, and there's no such thing designed in XAML as constructor parameters, because there's no need to do it: you specify properties in the tag. – EvAlex Jul 29 '13 at 07:57
4
  1. Go through and check all of the answers to this question - I'm sure one of them is right, but I don't know which one worked for me.
  2. Save your project, close Visual Studio, re-open Visual Studio.
  3. Voila, it now magically works, despite not changing anything.
  4. Swear at Microsoft under your breath, and carry on with your life...
Grim
  • 672
  • 4
  • 17
1

Ran into this error right now. The problem was that the class I was referencing from my assembly was in an assembly that had a higher framework level(4.7.2 against 4.6). Version alignment solved the problem.

  • Hey, I'm facing the same problem and so far none of the solutions worked. Could you please elaborate on what version alignment you did? – Zivxx Mar 13 '22 at 09:46
0

I just discovered another issue, that may cause this problem. It is allowed to use dots in project names, e.g.

FancyTrainsimTools.Desktop

is a valid project name. If you use folders in the project, you may get constructs like this:

FancyTrainsimTools.Desktop.Logic

If you define data templates referencing items in this folder, this results in error messages. I also used a hierarchical datatemplate, but this gives slightly different messages.

The workaround:

Rename your project by removing the dot:

FancyTrainsimToolsDesktop

Edit: I try to reproduce the issue again, in order to report a bug, but now it all works.... So mystery not yet solved and not clear what to do to create w ork around....

RudolfJan
  • 91
  • 1
  • 11
0

Another thing to check, since I just solved the same issue today... For some reason, we had a xaml file that was added with a build action of Resource instead of Page. In that file, and only that file, we had to use the assembly= in the namespace declaration to get it to work. It's been the same since that file was added 13 years ago, everything in it seemed to work just fine, and we didn't notice any issues until now.

What tripped us up was that we're creating a .NET 6 version of the DLL, and the new project has all the same code and xaml files but isn't a direct clone of the .NET Framework version of the project. The way it was set up, the XAML file in question is being compiled the same way as all the other files, which means that it can't have the assembly= part in the namespace. So for a little while, I was misled into thinking it might be a .NET 6 vs .NET Framework project difference. Luckily, I noticed that the XAML file was set to Resource in the old version of the project, and as soon as I fixed that, everything works fine in both versions of the project (with assembly= removed).

0

For .Net Framework.

Check in another class in case the missing class cannot be resolved even there, and it's in a NuGet, the problem can be in the reference HintPaths. Open the .csproj file in a text editor and see if the package reference matches the HintPath.

e.g:

<Reference Include="System.IO.Pipelines, Version=7.1.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
 <HintPath>..\packages\System.IO.Pipelines.7.0.0\lib\net462\System.IO.Pipelines.dll</HintPath>
</Reference>

HintPath needs to be updated to refer to the folder for version 7.1.0, as is the reference.

Sometimes Visual Studio doesn't update the HintPath which makes trouble.

Hossein Ebrahimi
  • 632
  • 10
  • 20