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?

1
  • Make sure to add the library reference to the application project Commented Mar 14, 2017 at 15:08

4 Answers 4

36

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.

Sign up to request clarification or add additional context in comments.

5 Comments

well, in deed i saw such a solution from somewhere but i didn't know from where to learn the name of my assembly...=)
Any other suggestion/ idea/ help??
i am afraid it doesn't work when i write xmlns:myns="clr-namespace:LibNameSpace;assembly=LibNameSpace"
Does your Assembly name contain any spaces? You should add a underscore for spaces I guess.
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=)
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).

2 Comments

+1 for the suggestion to rebuild. I was getting really frustrated with the persistent squiggly.
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
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.

1 Comment

Tried so many different things and this one finally did it! So simple
5

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.