Why are XAML namespaces URIs and what are they for? - wpf

I am skeptical regarding the XML namespaces. I don't know why we need these namespaces in all XAML applications.
xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation
xmlns:x=http://schemas.microsoft.com/winfx/2006/xaml
I understand the concept of namespaces as in System.Text, System.LINQ, etc. We use that in our classes because we have class libraries that sit on the same computer.
Questions:
It represents some URI. So WPF is standalone application. If the user of the app is not connected to Internet, whats the significance of it? First of all why do we need this?
What's the difference between the first and second line? What does xmlns:x represent?
Also, I see this error upon trying to access that URI
An error occurred while processing your request
What does this mean?

The URLs mentioned in the definitions of the namespaces have nothing to do with actual internet-connection or real existing URLs; this is just a convention used so the namespaces can be properly distinguished.
A XAML-namespace is just like a namespace in C++ or C#/VB.NET, it serves the purpose of logically grouping classes and other items.
In WPF/XAML you have a namespace xmlns=http://... which is the namespace for the controls WPF offers you by default. xmlns:x is a namespace to provide some special values like x:Null to represent a null-value.
You can define your own controls and name them TextBox, too, but you should create an own namespace for these in order to not interfer with the global namespace.
There is a nice article on MSDN about this topic.

It's the way those namespaces were named. That's all. You should not treat them as uri.
As for xmlns vs xmlns:x - the first one is the default namespace, which contains wpf controls, the second one is namespace whcih contains some additional declarations (x:Type, x:Name, etc).

I think the information provided in the accepted answer is not totally correct. The url which you see is not a namespace but a collection of namespaces grouped into one. Below are some of the namespaces from .net which are mapped by default namespace (url)
System.Windows, System.Windows.Automation, System.Windows.Controls, System.Windows.Controls.Primitives, System.Windows.Data, System.Windows.Documents

Related

Set resource URI of .xaml component

I have a .xaml UserControl named MyUserControl.xaml and I want to set its resource URI.
Per default, WPF generates a URI that includes a resource name, which is equal to the resource it belongs to such as
"/MyNamespace;component/myusercontrol.xaml"
for the .xaml named MyUserControl.xaml
How can I have a UserControl MyUserControl.xaml and make WPF generate an individual resource identifies such as
"/MyNamespace;component/myusercontrol_A.xaml" or
"/MyNamespace;component/myusercontrol_B.xaml" ?
The reason why I want to do that is described here.
In the image below you can see the resource identifier I am talking about:
and therein:
Note, that that question is the origin of this question and might help to understand its background.
After a week suffering and laboring with this issue, I finally found both the reason for the problem and its solution.
The problem lies within the auto-generated *.g.i.cs file, which is called by the InitializeComponent() method of a UserControl, as seen by the following:
This file generates a string (a Resource Locator) that expresses the path to that xaml-component, as seen by the following:
Now, if you have multiple versions of the same assembly and both versions include the same xaml-file, WPF does not know what xaml-file to instantiate, because the Resource Locator only references the name of the assembly but not its version.
This results in a TargetInvocationException, saying that
{"The component 'MyNamespace.MyUserControl' does not have a resource identified by the URI '/MyAssembly;comoponent/myusercontrol.xaml'"}
as follows:
The simple (but most definitely not obvious) solution for this is to add the version of the assembly to this Resource Locator. This can be achieved by modifying the build-file of the project by adding the <AssemblyVersion>-tag as follows:
Credits for this go to:
this blog
this SO thread

Cannot find namespace

I found this article when challenged with a problem in one of my applications. I understand the usage, but cannot find the namespace for the local element where we find local:SetterValueBindingHelper.PropertyBinding. So what namespace does this reference? Google has not found the class either, nor MSDN. Thanks for the help!
Local just means an alias for a namespace in your own (or someone else's) code. E.g. say you took the SettingValueBingingHelper and saved it in your project, in the namespace MyNamespace. To use it in xaml you would reference the namespace by adding to your UserControl (or Page)
xmlns:local="clr-namespace:MyNamespace;assembly=MyAssembly"
It does't have to be local - it could be named anything that is relevant to your code, e.g. helpers.
Another example would be using the Triggers from System.Windows.Interactivity - you would reference the namespace:
xmlns:i="clr-namespace:System.Windows.Interactivity;
assembly=System.Windows.Interactivity"
and then use the triggers in your xaml:
<i:Interaction.Triggers>
BTW, the link in your original post has been updated.

XMLNS : a way to set them once for all?

We have multiple namespaces and assemblies in my project, and so many xmlns to declare in each control we make (at least 7, without system ones like x: or d:). It's so tedious.
Is there a way to declare xmlns once for all in the application ?
If not, do you have some workarounds (in order to have a lightweight control definition) ?
Please note that I know XmlnsDefinitionAttribute, it's not always possible (with external ssemblies for example) or not a great design (for me it's not a great design to put an UI specific attribute in a business entity assembly).
xmlns are a document level thing needed by the compiler, they are like using statements which you will also have to place in every C# code file.
If you have many common xmlns you could create an item template for Visual Studio which adds them by default, or find an extension which allows for something like Snippets in XAML.

What are xmlns=[some url] attributes in the beginning of WPF xaml files for?

I noticed in the beginning of a xaml we have stuff like
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
The only other usage I see for xmlns is importing namespaces from my assemblies. Are these also instances of importing assembly? From a URL? If not, what do they refer to? What if the computer is not connected to the internet?
this is not related to xaml, but to xml in general.
The target of namespaces is to be able to uniquely identify xml objects.
take a look here.
For example, it allows to have two "Customer" node, with different namespaces. Programs can then distinguish if it's a customer node from system A or system B.
This can be compared to C# namespaces also. The "Control" class exists both in System.Windows and System.Windows.Forms and even in System.Web.Ui... same name, but "ownership" are different according the namespaces.
One thing to know, it's only a declaration. the namespace is a freetext, the http:// format is just a convention, and no download of the target uri will occur.
The first declaration maps the overall WPF client / framework XAML namespace as the default:
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
The second declaration maps a separate XAML namespace, mapping it (typically) to the x: prefix.
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
The relationship between these declarations is that the x: prefix mapping supports the intrinsics that are part of the XAML language definition, and WPF is one implementation that uses XAML as a language and defines a vocabulary of its objects for XAML. Because the WPF vocabulary's usages will be far more common than the XAML intrinsics usages, the WPF vocabulary is mapped as the default.
The x: prefix convention for mapping the XAML language intrinsics support is followed by project templates, sample code, and the documentation of language features within this SDK. The XAML namespace defines many commonly-used features that are necessary even for basic WPF applications. For instance, in order to join any code-behind to a XAML file through a partial class, you must name that class as the x:Class attribute in the root element of the relevant XAML file. Or, any element as defined in a XAML page that you wish to access as a keyed resource should have the x:Key attribute set on the element in question
http://msdn.microsoft.com/en-us/library/ms747086.aspx

In Silverlight why are some properties prefixed with x e.g x:name and some are not?

In Silverlight (and I guess WPF) why are the properties x:name x:fieldmodifier x:uid the only ones with the prefix x.
I understand the x prefix is used to refer to the XML namespace but there are a number of other properties that do not use a prefix such as width. Identifying a control is such a common task it seems odd to require a prefix?
I think that the key point here is the difference between xaml and wpf / SL.
Xaml is really a object initialization language and is totally independent from wpf / SL. For example you can also use xaml to define workflows in WF. I think these is the reason to have the “pure” Xaml namespace’s differentiated from presentation namespaces.
Update: I’m sorry because I don't understand well the question (see the comment).
In silverlight and wpf the .net namespaces are associated to xml namespaces.
The more common .net namespaces are a associated to the xml namespace
"http://schemas.microsoft.com/winfx/2006/xaml/presentation".
Then in the xaml root element there is this namespace definition:
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
These means that the .net namespaces associated to "http://schemas.microsoft.com/winfx/2006/xaml/presentation" doesn't need prefix.
Here is a good explanation of xaml and namespaces.
One of:
Personal preference.
The implementation (class) of that project is in the same assembly as the XAML file.
Some fool decided to create his own name property.

Resources