How to avoid XAML namespace import in every Window - wpf

Is it somehow possible to avoid xmlns:Engine="clr-namespace..." if i need the same namespace in each of my application windows?
Thanks

I'm agree with HCL, I think it can't be done and adding the namespace will not consume more resources.
I will give you an alternative. You can change the the window template (or create one of your own) to make it include the namespace for you.
In my computer, for VS2010, the path of the template is:
C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\ItemTemplates\VisualBasic\WPF\3082
Note that one is for vb.Net, there is a CSHARP directory as well.
Edit:
As far as I Know, the templates are for each computer, not for each project, but when you edit the template you can use conditionals, for example:
$if$ ($targetframeworkversion$ >= 3.5)using System.Linq;
You can use the $rootnamespace$ constant to check for a specific project and then add the namespace or not. Maybe a constant with the current project name exists, i don't know, but $rootnamespace$ should be enought for this pourpose.

If it's your own the sourcecode to the assembly you using in the Engine namespace you can accually totaly remove the prefix requerment and the demand of adding something. With this little trick you just append your own namespaces to the xaml namespace (that allready is included).
In your "Engine" project (not the project where you have the xmlns:Engine line, can't be the same project where you use it, it will not work then) make sure you have the WindowsBase reference added and then add the following in the the AssemblyInfo.cs/AssemblyInfo.vb file (c#: found in the Properties folder) (vb: you need to use the "Show all Files" button and then you will find it in the My Project folder):
c#:
[assembly: XmlnsDefinition("http://schemas.microsoft.com/winfx/2006/xaml/presentation", "The.Class.Namespace")]
vb:
<Assembly: XmlnsDefinition("http://schemas.microsoft.com/winfx/2006/xaml/presentation", "The.Class.Namespace")>
You can do this several times if you project have classes in more then one namespace you wanna use in the other projects xaml.

No, there is no way to avoid this. However declaring a namespace does not mean that it consumes resources (at runtime).

Related

Intillisense fails on undefined namespace in XAML on Visual Studio 2010

I must be brain dead. I have tried for six hours now (with Google) and can not solve this very very simple problem.
In Visual Studio 2010, I created a project. (right-click on solution, Add new project...).
Inside the new project, I created a new folder of name Helpers. To the folder I added a class for a converter.
Inside the project, I added a new XAML window (in WPF). The Target Framework is .Net Framework 4.0
Everything has been built (multiple times) without compilation errors. The platform target build for the project is X86.
The problem: Intellisense does not see the namespace for the class under the Helpers folder and so the Visual Designer fails. The namespace in the Helpers folder is:
Chaos.UI.Helpers
and trying to reference this namespace in the XAML like:
xmlns:converters="clr-namespace:Chaos.UI.Helpers"
ofcourse results in the dreaded:
Undefined CLR namespace. The 'clr-namespace' URI refers to a namespace 'Chaos.UI.Helpers' that is not included in the assembly.
These are clean files in the same project. The only added code is in the converter class contained in the Chaos.UI.Helpers namespace.
How do I fix this? (I need the designer to work).
Anybody, Plz help
Try use assembly
xmlns:converters="clr-namespace:Chaos.UI.Helpers;assembly=Chaos.UI"

Application Wide WPF Namespace

In my WPF Application I find myself adding the following namespace to every control:
xmlns:Core="clr-namespace:MHA.Modules.Core;assembly=MHA.Modules.Core"
Is there a way I can only add this namespace once and that it will be available application wide?
Thanx
Sadly there is no way that I know off to do this..
What you could do instead is use the XmlnsDefinitions which would tidy this up considerably and would also give you abit of of future proofing if you decide to add something new to the core but in a different name space.
in your AssemblyInfo file, add the following
[assembly: XmlnsDefinition("COMMON-URL", "MHA.Modules.Core")]
you can also add multiple namespaces under one URL.
then you could reference it in your XAML like
xmlns:Core="COMMON-URL"
for more information - see http://zachbonham.blogspot.co.uk/2010/04/organize-xaml-namespace-declarations.html
cheers.
ste.

I can't add reference to namespace system.windows.controls in a library project

I'm trying to add a reference to the namespace System.Windows.Controls in a library project but i can't find it in the list.
Does anybody know what is going on? i'm using 4.0.
thanks.
You need to learn to distinguish between assembly names and namespaces. They're not in a 1-1 correspondence.
If you're wanting to access e.g. System.Windows.Controls.Button, you need to reference the PresentationFramework assembly, which does seem to contain most of the classes documented on MSDN as being in the System.Windows.Controls namespace.
Multiple assemblies can contribute classes/controls to a single namespace. And there's no requirement that all (or even the majority) of classes from an assembly all reside within a single namespace.
If you are using VS 2010:
Check you project settings: right click your Project, select "Properties", then select the "Application" tab. If the Dropdown for "Target framework" is ".Net Framework 4 Client Profile" you have to change it to ".net Framework 4".
Edit: I guess the requried assembly is "PresentationFramework.dll" (see msdn class info about Usercontrol). My fault, Ijust thought it would the Client Profile, because I usualy make this error.
The System.Windows ships with Silverlight toolkit. Hope that helps.

The tag "xxx" does not exist in XML namespace"yyy" / Reference could not be found

I have a winform usercontrol than I want to add to wpf project.
I added the relevant references (WindowsFormsIntegration, SystemWindowsForms and my user control dll) and added this row in my XAML:
xmlns:MyControl="clr-namespace:xx.xx.xx;assembly=xx.xx"
And then this:
<WindowsFormsHost><MyControl:control></MyControl:control></WindowsFormsHost>
When I write "MyControl:" the "control" is automated show up that mean VS recognize the control and all references added ok... but when I compile the project this give me the error in the title.
Edit
Its very strange when I'm compile the all project i've got error "The type or namespace name "xx' could not be found..."
but I added all the refernces and the VS recognize the namespace so why the compiler don't found them? If this problem will be solved I beleive the other problem also will disappear.
The solution is: Go to your project properties and change the Target Framework from Client Profile to the full version of the .Net you are using, see the image below. This problem happens if your DLL targets the full .NET Framework, and your WPF Application (main project) targets .Net Client Profile.
This happened to me when I included the assembly name in the namespace definition, but both controls were in the same assembly. Just removing the assembly part of the namespace declaration solved it.
Visual Studio will load the reference only for reflection so it will show correct intellisense correctly. But in order to compile, compiler will need all the dlls that your referenced dll is dependent on. So visual studio will show intellisense for mycontrol as it can find it in reference. But your myontrol may reference other dlls which you may not have added. You will have to add dependent references of mycontrol in your project too.
It could also be that the target framework is different between your projects. We host a class library dll with the WPF pages, and it was targeted to 'Any CPU', but the host application was targeted to 'x86'. Once they matched, the problem went away.
I ran into the same "The type or namespace name 'xx' could not be found..." issue.
It disappears when I moved my Visual Studio files on a local drive. They were stored on a shared network directory before. I dont know the root cause of this, but at least I can have my designer running now.
I've encountered this problem before and replacing <MyControl:control></MyControl:control> with just <MyControl:control/> fixed it for me.
No idea why though. It seems like the first form is only for controls which can contain other controls.

IlMerge Silverlight Class Library with Custom Controls

I am trying to merge all the assemblies of an class library in a single .dll file.
I can merge all the assemblies using the Ilmerge but is that when I use the merged dll in a Silverlight application I am having trouble when a template is apply to my control and binding problems if I use a control that inherits with UserControl.
is there any solution to solve this problem?
The problem is that when the initial dlls are built the Xaml in the project is added as a resource in the dll. The code generated to load this xaml will look something like this:-
System.Windows.Application.LoadComponent(this, new System.Uri("/SilverlightLibrary1;component/MyControl.xaml", System.UriKind.Relative));
Note how the name of the dll forms part of the Uri need to fetch the xaml. I doubt IlMerge is able to spot that and fix it up. Hence once merged the Uris cannot be found.
A resolution for this is probably uglier than multiple references or simply creating another project that links all the code files involved.

Resources