wpf x:xaml 中的意思
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4573782/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
x: meaning in xaml
提问by Night Walker
I see a lot statements like
我看到很多像
<TextBox x:Name="txtInput" />
<TextBox x:Name="txtInput" />
or like
或喜欢
<BooleanToVisibilityConverter x:Key="boolToVis" />
Why the x:
is needed and what it gives me.
为什么x:
需要它以及它给了我什么。
<DockPanel.Resources>
<c:MyData x:Key="myDataSource"/>
</DockPanel.Resources>
And here we have also the c:
在这里我们还有 c:
Thanks for help
感谢帮助
回答by Dabblernl
It is nothing more than shortcuts to the different namespaces for XML. You can choose them as you like. If you look at the upper lines in your XAML you will find the line:
它只不过是 XML 不同命名空间的快捷方式。您可以根据需要选择它们。如果您查看 XAML 中的上面几行,您会发现以下行:
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Change the 'x
' to 'wpf
' for instance and you will see that you need to change all the 'x:
' prefixes in your code to 'wpf:
' to make it compile.
例如,将x
' wpf
'更改为 ' ' ,您将看到您需要将x:
代码中的所有 ' ' 前缀更改为 ' wpf:
' 以使其编译。
The 'c:
' prefix references code of your own. Say you have a class library that compiles to MyLib.dll. This library contains a class named MyData. To be able to reference the MyData class you need something like:
' c:
' 前缀引用您自己的代码。假设您有一个编译为 MyLib.dll 的类库。该库包含一个名为 MyData 的类。为了能够引用 MyData 类,您需要如下内容:
xmlns:c="clr-namespace:MyClasses;assembly=MyLib"
in your XAML header.
在您的 XAML 标头中。
You can then reference the MyData class in you XAML with c:MyData
. But you are entirely free to change the 'c
' to 'myfabulousclasses
' or anything else you fancy.
然后,您可以在 XAML 中使用c:MyData
. 但是您可以完全自由地将 ' c
'更改为 ' myfabulousclasses
' 或您喜欢的任何其他内容。
The purpose of this? To distinguish classes or members that have the same name, but belong to different dll's.
这样做的目的是什么?区分具有相同名称但属于不同 dll 的类或成员。
回答by Kishore Kumar
The x: Prefix
x:前缀
In the previous root element example, the prefix x: was used to map the XAML namespace http://schemas.microsoft.com/winfx/2006/xaml, which is the dedicated XAML namespace that supports XAML language constructs. This x: prefix is used for mapping this XAML namespace in the templates for projects. The XAML namespace for the XAML language contain several programming constructs that you will use very frequently in your XAML. The following is a listing of the most common x: prefix programming constructs you will use:
在前面的根元素示例中,前缀 x: 用于映射 XAML 命名空间http://schemas.microsoft.com/winfx/2006/xaml,这是支持 XAML 语言构造的专用 XAML 命名空间。此 x: 前缀用于在项目模板中映射此 XAML 命名空间。XAML 语言的 XAML 命名空间包含多个您将在 XAML 中经常使用的编程构造。以下是您将使用的最常见的 x: 前缀编程结构的列表:
x:Key: Sets a unique key for each resource in a ResourceDictionary (or similar dictionary concepts in other frameworks). x:Key will probably account for 90% of the x: usages you will see in a typical WPF application's markup.
x:Key:为ResourceDictionary(或其他框架中的类似字典概念)中的每个资源设置唯一键。x:Key 可能占您将在典型 WPF 应用程序标记中看到的 x: 用法的 90%。
x:Class: Specifies the CLR namespace and class name for the class that provides code-behind for a XAML page. You must have such a class to support code-behind per the WPF programming model, and therefore you almost always see x: mapped, even if there are no resources.
x:Class:为 XAML 页提供代码隐藏的类指定 CLR 命名空间和类名。你必须有这样一个类来支持每个 WPF 编程模型的代码隐藏,因此你几乎总是看到 x: 映射,即使没有资源。
x:Name: Specifies a run-time object name for the instance that exists in run-time code after an object element is processed. In general, you will frequently use a WPF-defined equivalent property for x:Name. Such properties map specifically to a CLR backing property and are thus more convenient for application programming, where you frequently use run time code to find the named elements from initialized XAML. The most common such property is FrameworkElement.Name. You might still use x:Name when the equivalent WPF framework-level Name property is not supported in a particular type. This occurs in certain animation scenarios.
x:Name:为对象元素处理后存在于运行时代码中的实例指定运行时对象名称。通常,您会经常为 x:Name 使用 WPF 定义的等效属性。此类属性专门映射到 CLR 支持属性,因此更便于应用程序编程,在应用程序编程中,您经常使用运行时代码从初始化的 XAML 中查找命名元素。最常见的此类属性是 FrameworkElement.Name。当特定类型不支持等效的 WPF 框架级 Name 属性时,您可能仍会使用 x:Name。这发生在某些动画场景中。
x:Static: Enables a reference that returns a static value that is not otherwise a XAML-compatible property.
x:Static:启用返回静态值的引用,否则该值不是 XAML 兼容的属性。
x:Type: Constructs a Type reference based on a type name. This is used to specify attributes that take Type, such as Style.TargetType, although frequently the property has native string-to-Type conversion in such a way that the x:Type markup extension usage is optional.
x:Type:根据类型名称构造一个类型引用。这用于指定采用 Type 的属性,例如 Style.TargetType,尽管该属性通常具有本机字符串到类型的转换,因此 x:Type 标记扩展的使用是可选的。
http://msdn.microsoft.com/en-us/library/ms752059.aspxhttp://msdn.microsoft.com/en-us/library/ms753327.aspx
http://msdn.microsoft.com/en-us/library/ms752059.aspx http://msdn.microsoft.com/en-us/library/ms753327.aspx
回答by Darin Dimitrov
It is part of a namespace. In your example the c: prefix
is used to indicate that the MyData
tag belongs to this namespace. You may take a look at the following articleon MSDN which explains the x: prefix
in XAML.
它是命名空间的一部分。在您的示例中,c: prefix
用于指示MyData
标签属于此命名空间。您可以查看MSDN上的以下文章,其中解释了x: prefix
XAML。