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.
Related
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
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.
I tried to use this link: http://msdn.microsoft.com/en-us/library/ff407130.aspx . But whenever I debug it for some reason it throws an error :
The type 'src:MyVirtualizingStackPanel' was not found. Verify that you
are not missing an assembly reference and that all referenced
assemblies have been built.
And another error:
'src' is an undeclared prefix. Line 13, position 22.' XML is not valid
I copied the class and methods into the MainWindow class and the XAML.
Am I missing anything here?
At the end of the day, you are mistaken about your XML Namespace. If you type your error minus the name into a search engine, you will see something:
The type was not found. Verify that you are not missing an assembly reference and that all referenced assemblies have been built
All of the results will point to the fact that you have not provided the correct XML Namespace for your project. Now we don't have your project in front of us... only you do, so only you can work out what your Namespace problem is. You mentioned something in a comment that made me suspicious.
You said The name-space is set automatically, but when I asked you to tell me exactly what that meant, you didn't, so I still don't know what you meant. Either way, it doesn't matter, but that could be a cause of your problem. Here are some other possible causes:
Maybe your MyVirtualizingStackPanel class is defined in a different class/project to where you specified.
Maybe you need to include the assembly=XXX part of the XML Namespace.
Maybe you have not included the CLR Namespace in your MyVirtualizingStackPanel class.
Either way, this is definitely an XML Namespace problem, so just keep looking/experimenting and you'll find the problem eventually. You can find out exactly how to add an XML Namespace from the XAML Namespaces and Namespace Mapping for WPF XAML page on MSDN.
Yes, you need to specify the namespace where the MyVirtualizingStackPanel class comes from.
In the beginning of your XAML, add:
xmlns:src="clr-namespace:XXX"
where XXX is the namespace.
See MSDN.
You have to import in the xaml the namespace srcto use this
On the top of the page you do:
xmlns:src="yourProjectPath"
than you can use the MyVirtualizingStackPane class.
add the XMLNS in the top,
Here look at the example
xmlns:toolkit="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls.Toolkit"
Add a reference to 'PresentationFramework' - you will find it in:
Add reference... -> Assemblies -> Framework
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"
Microsoft StyleCop provided a warning when the using directives for namespaces are provided outside of the namespace. Is this really required as my view on this is that using dircetives for namespaces is for providing a alias name for namespace and for removing the need for providing the namespace name when a class/interface is used. I dont think it will be used for loading the assembly.
It's basically your choice. But as we follow Microsoft and this is their standard it's good to have your all usings inside your name space. And also have them sorted.