13
votes

I am quite new on C#, WPF and XAML, so I may not be able to use the right terms to ask the right question =) 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?

4
Make sure to add the library reference to the application project - patrick

4 Answers

31
votes

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.

10
votes

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).

6
votes

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.

4
votes

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.