The situation I am encountering is the same as here:
WPF Custom Namespaces not working
XmlnsDefinition only works for namespaces in other assemblies, not in
the same assembly.
However, I don't really get what is exactly meant by "same assembly" and "different assembly". If AssemblyInfo.cs and the XAML files are in the same assembly, how can I move AssemblyInfo.cs away from the assembly to another?
Documentation does not refer to such any limitation.
Mapping CLR Namespaces to XML Namespaces in an Assembly
WPF defines a CLR attribute that is consumed by XAML processors in order to map multiple CLR namespaces to a single XAML namespace. This attribute, XmlnsDefinitionAttribute, is placed at the assembly level in the source code that produces the assembly. The WPF assembly source code uses this attribute to map the various common namespaces, such as System.Windows and System.Windows.Controls, to the http://schemas.microsoft.com/winfx/2006/xaml/presentation namespace.
The XmlnsDefinitionAttribute takes two parameters: the XML/XAML namespace name, and the CLR namespace name. More than one XmlnsDefinitionAttribute can exist to map multiple CLR namespaces to the same XML namespace. Once mapped, members of those namespaces can also be referenced without full qualification if desired by providing the appropriate using statement in the partial-class code-behind page. For more details, see XmlnsDefinitionAttribute.
https://learn.microsoft.com/en-us/dotnet/framework/wpf/advanced/xaml-namespaces-and-namespace-mapping-for-wpf-xaml#mapping-clr-namespaces-to-xml-namespaces-in-an-assembly
Related
I'm trying to understand C# namespaces. Suppose two namespaces exist with the same name and they have identical contents, i.e. all classes and methods also have identical names. How can the compiler distinguish between the two if I try to call a method, e.g. MyMethod in class MyClass in namespace MyNamespace? It's not likely a real-world scenario, but I find the information useful for understanding purposes.
Second question: When I dot on a namespace, e.g. System, I get a set of related namespaces like System.Configuration etc., but when I go to Microsoft's documentation I cannot find any namespace named Configuration contained in System namespace. Why is that? I can of course find the namespace if I look for System.Configuration, but I don't understand why the System.Configuration namespace is not nested inside the System namespace.
If you have two types that have the same name and same number of type parameters, and exist in the same namespace, then the assemblies that they are in must be different.
If your project references the assembly that one type is in, but not the other, then you will obviously only be able to refer to the type that is from the assembly that you referenced. If your project references both assemblies, then there will be a compiler error, like this. To fix it, you can use an extern alias.
Not being able to find System.Configuration in this page is merely due to how Microsoft organises their documentation. They have decided to list all the namespaces out here, rather than display a hierarchy. Note that System.Configuration is part of the .NET Platform Extensions, not plain old .NET, so you have to choose that in the dropdown.
I've two similar projects. One is a Silverlight project and the other one a WPF. Both of them containing some namespaces and plenty of custom user controls.
As the controls are distributed over many namespaces, I have to define quite a few namespaces, when I'm using them. So I started to define the XML namespaces in the AssemblyInfo.cs:
[assembly: XmlnsPrefix("http://ui.example.com/xaml/touch", "cui")]
[assembly: XmlnsDefinition("http://ui.example.com/xaml/touch", "example_ui.controls")]
[assembly: XmlnsDefinition("http://ui.example.com/xaml/touch", "example_ui.themes")]
Now I have to define only one namespace in each file:
xmlns:cui="http://ui.example.com/xaml/touch"
Unfortunately this only works in Silverlight. My question is, how do I get this to work in a WPF project?
In the WPF project I get errors like this:
Error 5 The tag 'LookUpField' does not exist in XML namespace
'http://ui.example.com/xaml/touch'. Line 7 Position 14. D:\src\prototype\lookup-control\lookup-control\MainWindow.xaml 7 14 lookup-control
Althougth the tutorial worked for Silverlight, I believe it may be inaccurate for WPF. See accepted answer here.
I have a separate assembly with custom controls in WPF and I'm able to use the XmlnsDefinition attribute without a problem. I think the problem lies within the definitions need to exist before the assembly is built. So even though controls will show up in your designer, it can't be built because the compiler needs the definitions while compiling the XAML in BAML which are built into the assembly at a later point.
Edit
I created another WPF user control library and added the same XmlnsDefinitions you used, added the reference of the user control library to my WPF application project, used <cui:UserControl1 /> in my MainWindow.xaml and had no errors.
Is it a general guideline to have at least one namespace per assembly?
In what case, should multiple assemblies generally share the same namespace?
Development Environment : C# and .NET
From MSDN
Assembly
An assembly is a collection of types and resources that forms a logical unit of functionality. All types in the .NET Framework must exist in assemblies; Each time you create a Microsoft Windows® Application, Windows Service, Class Library, or other application with Visual Basic .NET, you're building a single assembly. Each assembly is stored as an .exe or .dll file.
Namespace
Namespaces are not a replacement for assemblies, but a second organizational method that complements assemblies. Namespaces are a way of grouping type names and reducing the chance of name collisions. A namespace can contain both other namespaces and types. The full name of a type includes the combination of namespaces that contain that type.
The answer is -- it depends.
If your assemblies are all small components of a given project, they may not need their own namespaces if they are distinct, self-contained and all "fit" under the namespace for the overall project.
If you're building assemblies which are only tangentially related and could easily be used in a wide variety of projects, you may want to group these in their own namespace.
If you're creating a class which has similar functionality or duplicate members to an existing class in your project or the CLR, you'll want a namespace for that too.
I would suggest to let the namespace match it's file location. Try and install resharper, you will see what i mean.
I do not know of a case where assemblies should share the same namespace. Only the first part of a namespace should be the same, the name of the company or product.
See this post from Mark
And this post to tell Resharper to get around this.
I'm a bit tired of having to declare an xmlns in every xaml file and having to use prefixes for my custom controls. Is it possible to map a clr namespace to "http://schemas.microsoft.com/winfx/2006/xaml/presentation"?
I tried the following in my AssemblyInfo.cs:
[assembly: XmlnsDefinition("http://schemas.microsoft.com/winfx/2006/xaml/presentation","MyOwnNamespace")]
but this doesn't seem to work. I still get a compile error like:
The tag 'MyCustomControl' does not exist in XML namespace 'http://schemas.microsoft.com/winfx/2006/xaml/presentation'.
Note: My controls are within the same assembly (I have a single assembly).
Unfortunately, you cannot use controls mapped to a Xaml namespace defined by XmlnsDefinition, if the controls are defined in the same assembly. Different assemblies work fine though. You'll have to use the clr-namespace definition for this.
Why would you want to add your controls to the Xaml default namespace? Don't do this. It's like using the System namespace for your classes because you don't want to add using directives for their namespaces.
I'm trying to localize two different projects which share the same namespace.
The Resources files are in project A. Whenever I try to localize a control in project B I get this error:
Error 21 Cannot find the type 'Resources'. Note that type names are case sensitive. C:\data\projects\UIDesktop\MainWindow.xaml
For both I'm adding this reference at the top of each xaml
xmlns:properties="clr-namespace:Gui.Properties"
Is this possible or should they have different namespaces altogether?
thanx in advance
When you only specify the namespace (like "clr-namespace:Gui.Properties"), it refers to this namespace in the current assembly. If you have the same namespace in another assembly, you need to specify the assembly name:
xmlns:properties="clr-namespace:Gui.Properties;assembly=MyAssembly"