开发者

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

开发者 https://www.devze.com 2023-03-04 23:24 出处:网络
I noticed in the beginning of a xaml we have stuff like xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\"

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 connec开发者_JAVA技巧ted 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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号