14

I am trying to add my own namespace to my xaml file in order to use my own class easily -I guess the reason is this- I wrote the following code in window tag for this:

xmlns:myns="clr-namespace:LibNameSpace" 

Where my window tag also starts with the following definition:

<Window x:Class="LibNameSpace.MainWindow"

I want to use the LibNameSpace:Class1 class, and I was hoping to write myns:Class1 for this. However, that command causes this error:

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

How can I fix this?

SomeBody
  • 7,515
  • 2
  • 17
  • 33
cemregoksu
  • 841
  • 3
  • 14
  • 22

4 Answers4

35

The name LibNameSpace sounds like its a library in another assembly. If this is the case, you must add the name of the assembly:

xmlns:myns="clr-namespace:LibNameSpace;assembly=MyLibAssembly

Update:

The name of the assembly can be found in project-explorer in the properties-screen of the project (of the library-assembly). In general also the file-name of the dll without the dll-suffix represents the assembly name.

StayOnTarget
  • 11,743
  • 10
  • 52
  • 81
HCL
  • 36,053
  • 27
  • 163
  • 213
  • well, in deed i saw such a solution from somewhere but i didn't know from where to learn the name of my assembly...=) – cemregoksu Jul 21 '10 at 10:02
  • Any other suggestion/ idea/ help?? – cemregoksu Jul 21 '10 at 10:20
  • i am afraid it doesn't work when i write xmlns:myns="clr-namespace:LibNameSpace;assembly=LibNameSpace" – cemregoksu Jul 21 '10 at 10:31
  • Does your Assembly name contain any spaces? You should add a underscore for spaces I guess. – Ingó Vals Jul 21 '10 at 11:52
  • 1
    it's funny that when i tried to write the full name such as "clr-namespace..." by hand it didnt work but when i just picked the namespace from the intellisense list, it worked... thanks for help=) – cemregoksu Jul 23 '10 at 07:45
10

Because for me it's not really clear what you want to do, here another try:

If MyLibAssembly is the main namespace of your application and there in you have a Window named MainWindow and a class named Class1 that you want to instantiate in your MainWindow-class:

  • Make sure, that in Class1 is no error, the project must compile without errors. Remove first the namespace-declaration from the xaml and compile your project till you have no compilation errors.
  • Make sure that Class1 is public and has a paramterless constructor
  • Make sure that in the code behind your MainWindow is also in the MyLibAssembly-namcespace.
  • Add then the namspace-declaration xmlns:local="clr-namespace:LibNameSpace into your xaml. local is generally used to declare the same namespace as your current element, in your case the window, is in.
  • Insert your Class1 with the <local:Class1/> -tag in the xaml. If Class1 does not derive from FrameworkElement or a higher level control, you must add it into the resources-section of your window. If this is true, give it a key. <local:Class1 x:Key="KeyToYourClass"/>

Maybe vs is out of sync. Click in the solution-explorer on the root-node Clean Solution and then Rebuild Solution. Maybe that helps.

I hope this helped. If not, try to reformat your question (use the code-symbol to make the question more readable and try to rephrase to make more clear what your desire is).

HCL
  • 36,053
  • 27
  • 163
  • 213
  • 2
    +1 for the suggestion to rebuild. I was getting really frustrated with the persistent squiggly. – Grault Mar 08 '14 at 18:17
  • from UserControl.xaml, the issue is to delete the standard file:UserControl.xaml.cs\vb; then have the happening under a file MyClass.cs\vb where MyClassNamespace.MyClassControls.UserControlClass works like UserControl.xaml.cs\vb – H3sDW11e Aug 08 '19 at 18:32
6

Use Intellisense. In my case one space mattered. instead of

xmlns:local="clr-namespace:DataAccess;assembly=DataAccess"

I hand typed

xmlns:local="clr-namespace:DataAccess; assembly=DataAccess"

Notice the space after ';'. This made the difference. So use visual studio Intellisense and it will render you correct xaml markup.

slavoo
  • 5,798
  • 64
  • 37
  • 39
gopal
  • 61
  • 1
  • 1
4

I found this answer while I was struggling with problems in Windows 8. I was trying to use a User Control and I had several errors. The last ones where:

Error 9 Cannot add 'ScrollControl' into the collection property 'Children', type must be 'UIElement'

and:

Error 10 Unknown type 'ScrollControl' in XML namespace 'clr-namespace:EventTests.Controls;assembly=EventTests, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'

ScrollControl is my user control.

I ended up replacing this:

xmlns:Controls="clr-namespace:EventTests.Controls"

For this:

xmlns:Controls="using:EventTests.Controls"

I hope this saves the time I spent with this issue.

Tiago Almeida
  • 14,081
  • 3
  • 67
  • 82