13

I've been researching for a while now trying to find a reason why the following would be occuring, but no solutions on StackOverflow or Google are able to help me.

I have a custom UserControl that is attempting to reference a namespace within the same project:

xmlns:my="clr-namespace:ColorPicker"

however when I compile I get the following error:

Undefined CLR namespace. The 'clr-namespace' URI refers to a namespace 'ColorPicker' that is not included in the assembly.

This is resulting in not being able to build my project or reference other custom controls within the xaml, generating these kinds of errors:

The type 'my:ColorSelector' was not found. Verify that you are not missing an assembly reference and that all referenced assemblies have been built.

I've attempted all the solutions given in these posts:

adding a custom namespace to xaml

WPF xmlns: The 'clr-namespace' URI refers to a namespace that is not included in the assembly

The 'clr-namespace' URI refers to a namespace that is not included in the assembly

Undefined CLR namespace

Also, just to be clear, I'm not getting any other errors about other files in this project, so it doesn't seem like it could be the result of other files not compiling.

UPDATE: A sample project that produces the error for me can be downloaded here: http://www.filefactory.com/file/28fbmhj3f4qj/n/ColorPicker_zip

Community
  • 1
  • 1
flamebaud
  • 958
  • 1
  • 8
  • 25
  • 1
    Hard to say from this description. Can you share a small sample project that repros the error? – Joe Castro Jun 14 '12 at 19:31
  • 2
    Are you sure the ColorPicker has the namespace as 'ColorPicker', have you tried adding the ;assembly=YourAssembly? – Kevin DiTraglia Jun 14 '12 at 19:33
  • I have no issues sharing the actual project. Are you expecting a file or just a bunch of source? If the actual project, what's the fastest method to post on here? – flamebaud Jun 14 '12 at 19:39
  • @flamebaud I often see users upload the entire solution to some free file sharing site online, and include the link in their question. – Rachel Jun 14 '12 at 20:08
  • Are you able to take your user control out of your current project and add it to a blank WPF project? If so could you try and tell us if you're able to reference it in that project, that'll totally rule out any build errors that aren't showing up in the control. – Andy Jun 14 '12 at 20:26
  • 2
    So you have a **namespace** called `ColorPicker` (because that sort of sounds like it could be a class name)? And in that namespace you have a class named `ColorSelector`? Finally the namespace `ColorPicker` is not nested under any other namespace? – Tod Jun 15 '12 at 00:24

3 Answers3

6

Your first linked question has the answer. The answer is: you have to build the assembly containing the namespace and referenced classes/controls before you can reference it in .xaml. I commented out your xaml namespace declarations, then commented out the xaml elements from those namespaces, then commented out the C# code that broke as a result of those elements no longer being declared. In other words, I kept commenting till I could build successfully. Once the assembly built, I uncommented the xaml namespace declarations, then the elements. This gave an error about needing to use x:Name instead of Name on those elements, so I did so. Then uncommented the C# code and it builds.

Community
  • 1
  • 1
xr280xr
  • 10,471
  • 6
  • 68
  • 110
  • Thanks so much @xr280xr, this process solved my solution, although it seems like an incredible long and annoying process to have to go through. Do you happen to know the reason why I need to go about it this way? That is, shouldn't Visual Studio be smart enough to do this itself? – flamebaud Jun 15 '12 at 16:17
  • 1
    @flamebaud I don't know how VS is works, but I'm assuming when you reference the namespace in the designer it loads the containing assembly. If the assembly hasn't been built yet, then your changes won't be in it and won't be available to your XAML; you'll be referencing something that doesn't exist yet. Now if you try to build the assembly to fix that, it can't because your broken XAML with the missing reference is compiled to the same assembly. VS is not smart enough to know to exclude the broken reference in order to build the rest of the assembly in order to fix the broken reference. – xr280xr Jun 15 '12 at 19:47
  • 1
    It would be nice if it was and it's a little misleading because intellisense is smart enough to pick up your changes without building first. As long as you remember to build your code changes before adding them to XAML it's usually not too big of a deal. – xr280xr Jun 15 '12 at 19:49
  • So is the only solution to put all those classes in a different assembly? – Farinha Jun 29 '15 at 08:28
1

xmlns:my="clr-namespace:ColorPicker;assembly=ColorPicker".

This worked for me! It's faster and less annoying!

Cezar
  • 52,020
  • 18
  • 84
  • 86
Heyjee
  • 99
  • 1
  • 14
0

I had to move mine to an external resource dictionary. I fixed my issue by changing:

<UserControl.Resources>
    <SHCL:GoodBadConverter x:Key="GoodBadConverter"/>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="pack://application:,,,/StandardHelperClassLibrary;component/WPF/Dictionaries/WindowStylesDictionary.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</UserControl.Resources>

to

<UserControl.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="pack://application:,,,/StandardHelperClassLibrary;component/WPF/Dictionaries/WindowStylesDictionary.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</UserControl.Resources>

where was moved to one of the resource dictionaries listed.

VoteCoffee
  • 3,300
  • 26
  • 31