为什么在XAML XmlDataProvider中我们必须说"x:Key"而不是"Key"?

Edw*_*uay 1 wpf xaml

命名空间的功能在这里是什么?

我想在这个简单的例子中我可以放"Key"和"XData"而不是"x:Key"和"x:XData",但是当我这样做时,在XmlDataProvider中找不到"Key".

<Window x:Class="DataBindingWPF.XmlBinding"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="DataBindingWPF" Height="300" Width="300"
    >
    <StackPanel>
        <StackPanel.Resources>
            <XmlDataProvider x:Key="Colors" Source="Colors.xml"  XPath="/colors"></XmlDataProvider>
            <XmlDataProvider x:Key="MoreColors" XPath="/colors">
                <x:XData>
...
Run Code Online (Sandbox Code Playgroud)

Mic*_*cah 7

"Key"属性不是XmlDataProvider的实际属性.它是一个Xaml属性,位于以"x"为前缀的" http://schemas.microsoft.com/winfx/2006/xaml "命名空间中.

StackPanel.Resources是一个ResourceDictionary.为了向字典添加内容,您需要一个键/值对.Key属性是资源的键,XmlDataProvider是值.这是WPF用于唯一标识资源的机制.

您可以将xmlns:x ="http://schemas.microsoft.com/winfx/2006/xaml"更改为xmlns:ns ="http://schemas.microsoft.com/winfx/2006/xaml"然后您将将其称为"ns:Key"而不是"x:Key".