In WPF, x: What is the difference between the Name and Name attributes?

The title says it all. Sometimes it seems Nameand x:Nameattributes are interchangeable.

So, what is a clear distinction between them? When to use one over the other one do?

Use the wrong way if they have an impact on performance or memory?


#1st Floor

They are not the same thing.

x:NameIs a xaml concept, mainly used to reference elements. When the elements to provide x: Name xaml when attribute "specified x:Name. Be created in the name of the field when processing code base XAML, and the field contains a reference to the object" ( the MSDN ) Accordingly, it is a designer generated fields by default with internal access.

NameIs FrameworkElementthe property of the existing string, as with any other properties xaml wpf listed together with element properties.

As a result, this also means that x:Namecan be used on a wider range of objects. This is a way for anything in xaml can be given names referenced technology.


#2nd Floor

The only difference is that if the user controls for Same Assembly in controls, the Name will not be able to identify your control, and the error will be "x is the use of the same control in the Assembly: Name". Therefore x: Name is named WPF Controls WPF version. Name only as Winform Legacy. They want to distinguish controls in Winforms and WPF named because they use the Xaml property from another using x: represents the name of the control program focused on the identification of controls.

Remember, not just to retain the name of the control and name it, because it resides in memory, it warns you that the name has been used to control, but never used as a blank.


#3rd floor

Name :

  1. Only for descendants of FrameworkElement and FrameworkContentElement;
  2. () Can code for similar properties and is set in the background SetValue.

x: Name :

  1. In almost all XAML elements;
  2. Can not be the SetValue () provided from a background code; instruction as it is, it can only use the property syntax disposed on the object.

Instructions to use a two or FrameworkElement FrameworkContentElement in XAML will cause an exception: if the flag XAML is compiled, the exception occurs at compile time tag would otherwise occur when loading.


#4th floor

One answer is x: name will be used in different programming languages ​​(e.g., C #), the name for the frame. Honestly, this is what I feel.


#5th Floor

x:NameMeans: create a field in the code behind to hold the reference to that object.

NameHe said: Sets the name of this object's properties.


#6th floor

Specified x: Name becomes the name of the field created XAML processing on the basis of the code, and the field contains the reference to the object. In Silverlight, using the managed API, the creation of this field is performed by the MSBuild target steps, which is also responsible for coupling together XAML file and its code-behind partial class. This behavior is not necessarily specified XAML language. Silverlight suitable for use in its programming and application model x: Name of the particular implementation.

Read more about the MSDN ...


#7th floor

When you declare a Button element in XAML, you referenced class Button is defined in Windows is running.

The buttons have a number of attributes, such as background, text, margins, ..... and a property name.

Now, when you declare a Button in XAML, as an anonymous object is created, it happens to have a property called Name.

Typically, you can not reference an anonymous object, but in the framework of WPF, XAML processor so that you can refer to the object by any value for the Name property specified.

Until now very good.

Another way to create an object is to create a named object instead of anonymous objects. In this case, you can set the XAML namespace (which may be named attribute) property of an object having called Name (as it in XAML namespace, thus having X :), so that the object can be identified and subjected to references.

in conclusion:

It is properties of the specific object, but X: Name is a property of the object (with a defined generic object class).


Building # 8

If you have a custom control, the X: Name may cause memory problems. It will retain a storage location for NameScope entry.

I said, unless absolutely necessary, do not use x: Name.


House # 9

variable. My research is x:Nameused as a variable. variable. However, Nameas a variable. Does this mean x: Name can call it anywhere in the XAML file, but not Name.
example:

<StackPanel>
<TextBlock Text="{Binding Path=Content, ElementName=btn}" />
<Button Content="Example" Name="btn" />
</StackPanel>
<TextBlock Text="{Binding Path=Content, ElementName=btn}" />

You can not name Buttonattribute Content Bindingis "btn", because it is located StackPaneloutside the


#10th floor

I always use x: Name variants. I do not know if this will affect the performance, I found it very easy for the following reasons. If your own user controls reside in another assembly, the "name" attribute is not always enough. This makes the paste x: Name property is also easier.


House # 11

They are the same thing, a lot of frame elements themselves disclose the name attribute, but for those who do not include them, you can use x: name- I usually insist on the use of x: name, because it applies to all situations.

The controls may be required for the name itself disclosed dependency attribute (because they require the use inside the dependency property), you may choose not to do so.

In MSDN for more details here and here :

Some WPF framework level application may be able to avoid the use of x: Name property because the WPF namespace specified in several important base classes (e.g. FrameworkElement / FrameworkContentElement) Name dependency property meets the same purpose. There are still some common XAML and framework program, which requires an element that does not have a Name property code access, especially in certain animation and storyboard support classes. For example, if you want your code references x: the Name, you should create in XAML and convert timeline specify them.

If the Name property can be used as a class, Name and x: Name can be used interchangeably as the attribute, but if you specify both simultaneously on the same element, an error will result.


House # 12

x: Name and Name references a different namespace.

x: name is a reference to the default namespace defined x on the top of the Xaml file.

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

Just say Name Use the following default namespace.

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

x: Name indication has x alias name space. x is the default value, most people keep it, but you can change it to any value

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

So your reference would be foo: name

Define and use namespace in WPF


OK, Let's another way to look at. Suppose you drop it on a button Xaml page. You can x: name and name referenced in two ways. All xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation" and xmlns: x = "http://schemas.microsoft.com/winfx/2006/xaml" are multiple naming reference space. Since xaml have Control namespace (not 100%), while representation has FrameworkElement , and Button class inheritance mode:

Button : ButtonBase
ButtonBase : ContentControl, ICommandSource
ContentControl : Control, IAddChild
Control : FrameworkElement
FrameworkElement : UIElement, IFrameworkInputElement, 
                    IInputElement, ISupportInitialize, IHaveResources

Therefore, as one would expect, any inherited from FrameworkElement things will have access to all its public properties. Thus, for a Button, which obtains its Name property from the top of the hierarchy tree of FrameworkElement. So, you can say x: Name or the Name , they are accessed from FrameworkElement getter / setter.

MSDN reference

WPF XAML CLR property definitions used by the processor to the plurality of CLR namespaces mapped to a single XML namespaces. XmlnsDefinitionAttribute property is in assembly level generated source code assembly. WPF assembly source code uses this property to map a variety of common namespace (e.g. System.Windows and System.Windows.Controls) to http://schemas.microsoft.com/winfx/2006/xaml/presentation namespace.

Therefore, the assembly attributes will look like this:

PresentationFramework.dll-XmlnsDefinitionAttribute:

[assembly: XmlnsDefinition("http://schemas.microsoft.com/winfx/2006/xaml/presentation", "System.Windows")]

[assembly: XmlnsDefinition("http://schemas.microsoft.com/winfx/2006/xaml/presentation", "System.Windows.Data")]

[assembly: XmlnsDefinition("http://schemas.microsoft.com/winfx/2006/xaml/presentation", "System.Windows.Navigation")]

[assembly: XmlnsDefinition("http://schemas.microsoft.com/winfx/2006/xaml/presentation", "System.Windows.Shapes")]

[assembly: XmlnsDefinition("http://schemas.microsoft.com/winfx/2006/xaml/presentation", "System.Windows.Documents")]

[assembly: XmlnsDefinition("http://schemas.microsoft.com/winfx/2006/xaml/presentation", "System.Windows.Controls")]  

House # 13

It is not WPF item, but XML standard items, BtBh correctly answers the question, x indicates the default namespace. In XML, when you do not add the namespace prefix for the element / attribute, it assumes that you will need to default namespace. Therefore, the only input Nameis just x:Nameshorthand. For more details on XML namespaces, see the link text.


House # 14

XAML is actually only one name x:Name. By the use of the class RuntimeNamePropertyAttribute(class attribute as one of the maps to the XAML x:Nameproperties), its attributes can be mapped to one frame using WPF XAML like x:Name.

The reason for this is to allow the runtime framework has been the concept of "name", such as WPF. For example, the WPF, FrameworkElementintroduced Name property.

In general, the class need not be stored x:Namecan be used. For XAML, all x:Namemeans to generate a field to the value stored in the back of the class code. Running the mapping operation depends on the frame.

Why, then, there are two ways to do the same thing? The simple answer is because there are two concepts mapped to a property. WPF want to keep the name of an element (such as the use by Bind) at run time, and XAML needs to know which elements you want to access through the back of the class in the code field. WPF marked by the Name property x: Name aliases associated with these two.

In the future, XAML will be used more for x: Name, for example, allows you to set the property by reference other objects by name, but in 3.5 and earlier, it is only used to create the field.

Should you use a style or another is indeed a problem, not a technical problem. I leave it to others to make recommendations.

See AutomationProperties.Name and the X-VS: the Name , accessibility tools and test tools use some AutomationProperties.Name.

Original articles published 0 · won praise 0 · Views 2219

Guess you like

Origin blog.csdn.net/p15097962069/article/details/103919768