Adding a custom namespace to XAML - c#

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?

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.

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

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.

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.

Related

WF - myCustomType cannot be converted to myCustomType

I got this strange error from time to time when an workflow is activated:
Object of type
'System.Activities.InArgument1[myNamespace.myCustomType]' cannot be
converted to type
'System.Activities.InArgument1[myNamespace.myCustomType]'
I am trying to understand where this error comes from and I am thinking somehow a different version of the dll is loaded, but still I cannot explain why this happens and how this could be fixed.
It sounds like a mismatch. One thing to check is hit F7 when you're on the XAML designer to look at the raw XAML and find your code activity that is complainig. The inArguments should have a type like x:TypeArguments="local1:MyCustomType"
Then scroll to the top and look at the xmlns declaration and make sure it matches the namespace you expect xmlns:local="clr-namespace:MyNamespace.MyCustomType". Also check for dupes in that list.

WPF xaml objects from dynamic assembly

I am working on some kind of interoperability library and stuck with the following issue.
My interoperabilty library reflects classes from other language in a dynamic assembly with Emit. And no classes are available at the build time. When I try to put the dynamic assembly into XAML with something like this:
xmlns:test="clr-namespace:ClassesReflected;assembly=ClassesReflected"
and then get a class from there with this
<test:TestDelegate x:Key="dyn" x:Name="dyn"/>
I have an error at compile time.
Is there anyway to bypass the compile-time checks or load the xaml at runtime or anything else to make this solution working?
Any tips. Highly appreciated.
I'm not sure if this works for you but there is one way.
First you need to define a namespace and ignore it:
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:validation="http://schemas.microsoft.com/winfx/2006/ololo"
mc:Ignorable="validation"
Then in code:
[assembly: XmlnsDefinition("http://schemas.microsoft.com/netfx/2007/xaml/
ololo", "SuperOlolo")]
Then you can use this namespace however you like:
<validation:ValidationSummary Style="{StaticResource ValidationSummaryStyle}"
Width="300" />
This will provide a clear build.
This method is used by Designer to read design-time peoperties.
I don't know if it can provide everything you need on run time may be some hacks will help you.
At least you could use a wrapper interface/class to make the members visible that you want to access in XAML.

Warning message when created a new Form

When i added a new from to my existing project, It shows this Warning Message
The type 'Banking_and_Financial_System.UserLoginForm' in 'D:\Internship
Project\Banking_and_Financial_System\Banking_and_Financial_System\UserLoginForm.cs'
conflicts with the imported type
'Banking_and_Financial_System.UserLoginForm' in 'D:\Internship
Project\Banking_and_Financial_System\Banking_and_Financial_System\bin\Debug\Banking_and_Financial_System.exe'.
Using the type defined in 'D:\Internship
Project\Banking_and_Financial_System\Banking_and_Financial_System\UserLoginForm.cs'.
in the Program.cs file on the Line below
Application.Run(new UserLoginForm());
Then I tried to modify the Existing form changed its name to Writeoff.cs then compiled it, I got the same above warning message.
This tells you that:
In UserLoginForm.cs, there is a type called Banking_and_Financial_System.UserLoginForm,
there is also a type calle Banking_and_Financial_System.UserLoginForm in Banking_and_Financial_System.exe
These two sound like the same thing to me, but it seems as if the latter of them is refering to a compiled version of your application. I find that a little strange, and I'm not sure about the technical details here, but it sounds like an accdidental inclusion causing a conflict.
Did you really mean to refer to the .exe-file in your debug-folder? If you've added such a reference explicitly, you should try to remove it.
Update:
Explanation: You can refer to and access public classes directly from a compiled .exe.
This has nothing to do with the file names of the forms and to do with the namespaces of the files.
You should specify the namespace for this call
Application.Run(new [insertcorrectnamespace].UserLoginForm());
What the error is telling you is it doesn't know which user login form you want to use as the class has knowledge of two with the same name but different namespaces

Do I need two xmlns:local="clr-namespace"?

Here's the setup I'd like to have for my Windows Phone app, using c# in visual studio 2010:
**MainPage.xaml** contains elements that have an attached property, whose values will be modifiable/savable by the user.
**MainPage.xaml.cs**
first Namespace is PhoneApp ,inside it is a nested namespace called MyNamespace that declares the dependency property. it works(Thanks, Daniel)
**SettingsSample.xaml** that will allow users to change the values of the attached property in MainPage.xaml for any element and automatically save the change.
**AppSettings.cs** a class that exactly reproduces the first listing in this tutorial:
http://msdn.microsoft.com/en-us/library/windowsphone/develop/ff769510%28v=vs.105%29.aspx
That page declares the same NameSpace as the MainPage.xaml.cs (PhoneApp), then a public class called AppSettings that is exactly like in the tutorial.
To join everything together, I did:
**MainPage.xaml**
xmlns:local="clr-namespace:PhoneApp.MyNamespace"
I needed this to use the attached property
<phone:PhoneApplicationPage.Resources>
<local:AppSettings x:Key="appSettings"></local:AppSettings>
</phone:PhoneApplicationPage.Resources>
Confusion begins. On the tutorial, they put this on the settings page, but I guess because their settings page is also the one including the elements with the properties that are bound to the saved settings. Mine are on the mainpage, so I put this here. To recap, My settings page will only use methods to change/save these values(and the methods are in AppSettings.cs). Also in the tutorial they add this:
xmlns:local="clr-namespace:SettingsSample"
to the Setting Page(where "SettingsSample" is the Namespace containing declaration/get-Set methods of savable settings) but, for the same reason, I tried to put it on the mainpage, but only one declaration of xmlns:local can be done. I tried several things to put them one after the other, but it doesn't work. This is the key to the two errors I'll list below.
Some elements of mainpage have this, for exemple:
local:MyClass.Son="{Binding Source={StaticResource appSettings}, Path=son1, Mode=TwoWay}" Style="{StaticResource pad}"
"Son" is the attached property
Ok, so I tried different different things but it never worked. The best I could get was in MainPage.xaml that it couldn't create an instance of AppSettings. Now it's different, I have the two errors.
-the type local:AppSettings was not found
-the tag AppSettings does not exist in xml namespace PhoneApp.MyNamespace.
I think this is because I didn't put the
xmlns:local="clr-namespace:PhoneApp"
But I already have
xmlns:local="clr-namespace:PhoneApp.MyNamespace"
and can't put both.(and to me, one is included in the other...) The reason I listed all the ins and out of the situation is because I kind of expect other troubles after I get through this.
I hope this message is clear enough for someone to help me. I spent so much time on it that I begin to loose my mind, so I hope there's no stupid mistake. Of course, I can add any information needed. Thank you for reading anyway!
These are XML namespace mappings. With the following:
xmlns:local="clr-namespace:PhoneApp"
The local part is the XML namespace, whilst PhoneApp is the namespace from your .NET code. With this definition in place you can then reference classes from this namespace in XML as follows:
<local:MyClassInPhoneAppNamespace/>
Because the local part is simply a name, you can change it to whatever you like:
xmlns:fish="clr-namespace:PhoneApp"
And use as follows:
<fish:MyClassInPhoneAppNamespace/>
This should mean that you no longer have collisions.
"local" in this case is simply a friendly name for the namespace you are referencing. It is completely interchangeable.
I was in need to import two local in same file as below
xmlns:local="clr-namespace:Generique.Views.Assets.Entries"
xmlns:local="clr-namespace:Generique.Views.Assets"
I just change the name and it works fine
xmlns:local="clr-namespace:Generique.Views.Assets.Entries"
xmlns:footer="clr-namespace:Generique.Views.Assets"

C#/.NET How do I find the containing namespace from a class name

I often find myself remembering the name of a class that I want to use, but not remembering the containing namespace.
Apart from searching the web, i wonder if a good method exists for looking this up.
I think if you press ALT, SHIFT and F10 in Visual Studio - intellisense will drop down an option for you to add the name space of the class you have just typed.
CTRL + '.' will bring up a menu where you can either add a 'using' or fully qualify the class.
You can always hang a big poster on your cube wall like me.
3.5 NameSpace
If you know the name of a class in .Net but have no idea what namespace it is in, it can be hard finding it, especially if you dont have a reference/using to the assembly containing it.
This is where the Object Browser (Ctrl+W,J) comes in handy.
Open it up, type in the name, it will give you all matches, either within your project/solution, or all of the .Net framework.
Edit:
As S.C. Madsen's comment points out, this also helps if you only remember PART of a class name, also if you only remember a method name but not the class.
Use the search function in .NET Reflector by Red Gate Software.
I generally use the offline MSDN reader, with the left panel set to the Index tab.
Another option in Visual Studio is to type the name of the type as if you were declaring a variable, and then see what it suggests. If the name goes to a light blue colour (by default) then it's in one of the namespaces you're already importing - just hover over it to find out which. Otherwise, see what namespaces it offers to add using directives for.
You can right click and select "Go To Definition" in VS and this will either load the class definiftion in your solution or it will show a metadata view of the class definition using reflection. Either of those should have the namespace defined near the top of the page.
If you need to add the namespace with a using decliration right click the unresolved class and mouse over to resolve. It will show you a list of namespaces that contain that class and selecting one will generate the using statement.
Two ways that work in Visual Studio 2013:
Right-click and select "Resolve".
Hover over the class and a 'Options to help bind the selected item' box will appear (same as Ctrl + '.' or Alt+Shift+F10)
Select the namespace and it will insert it for you.

Categories

Resources