What is x: a thing for in WPF?

I have seen this a lot and I do not know what that means. I would say this has something to do with namespaces? I was already looking for him, but it was not clear to me what this goal was.

Can someone explain what it is and in what situations it is commonly used?

thanks

change

<Window x:Class="WpfApplication8.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525"> <Grid> </Grid> </Window> 

But, for example, in the above code, I define x as an alias for the XAML namespace in the third line, although I use the same x on the right in the first line of code. How did this happen? He doesn't care about the order in which things appear?

edit2: Correct me if I am wrong:

Window x: Class = "WpfApplication8.MainWindow"

In the above code, the class received from the window in the namespace x: WpfApplication8.MainWindow will be placed, and

Window x: Name = "abc"

places an instance of the Window class named abc in the namespace x. Is it correct?

If I’m right, if I didn’t use the alias x , where would both the class (first case) and the instance (second) be introduced? Nowhere, a bit like anonymous types? They are used, but where are they not defined?

+4
source share
2 answers

You're right, this is an alias of the XML namespace. If you look at the top of your xaml file, you will find that it maps to the namespace http://schemas.microsoft.com/winfx/2006/xaml . Using the prefix later, allows the xaml parser to find the classes defined in this namespace without having to enter the whole thing. This is not like the syntax using alias = very.long.namespace; in c #.

Typically, you need to configure a different alias for each namespace that you intend to use in the xaml file. Normal when using PRISM maps cal to clr-namespace:Microsoft.Practices.Composite.Presentation.Regions;assembly=Microsoft.Practices.Composite.Presentation ; you can access the RegionManager class defined in this CLR namespace with cal:RegionManager . When using Expression Blend, you often find that it adds xmlns:d="http://schemas.microsoft.com/expression/blend/2008" and xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" to allow some of its design-time features (setting the width and height for a custom control in a dashboard).


The answer to the question:. You can take a look at the tutorial on the XML space in which xaml inherits this functionality. In your example, you need to stop thinking of strings as individual instructions, since they will be in the procedural code and will rather consider the element as a whole. What I'm trying to say is that since xmlns:x appears in the Window element, it is available for this element and for every child element it contains, the actual line it appears on is not important, because xaml is not executed from top to bottom, like procedural code.


The answer to edit 2: x: the class attribute tells the compiler that the name of the partial class created from the xaml file should be (if this is what you mean by “will put the class obtained from Window ...”, then you are right) .

You disconnect in the second part, the x: Name attribute tells the compiler to generate a field in the class containing a link to the element to which the attribute is attached. This means that in your example, your code behind the file will be able to use this.abc to refer to the Window element defined in xaml markup.

+11
source

This is an alias for the namespace definition.

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

It acts as a short statement or using / import .

By defining the namespace in the tag attributes, ala x:Name , you guarantee that you are using the correct attribute in the correct space.

You may have noticed that you need to add your own namespace alias if you want to use your own WPF control.

+2
source

Source: https://habr.com/ru/post/1301532/


All Articles