为什么我不能在我的根控件的资源中放置一个对象数组?

时间:2011-12-16 08:47:08

标签: wpf arrays xaml instantiation

假设我有一个带有两个额外xml的普通Xaml文件,一个带有两个CLR属性“Name”和“Age”的“Person”类,一个带有一个String对象:

<Window x:Class="WpfPractice.ListBinding"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:WpfPractice"
    xmlns:sys="clr-namespace:System;assembly=mscorlib"        
    Title="ListBinding" Height="200" Width="600">

我可以在我的根窗口元素的Resources集合中放置一个字符串数组:

    <Window.Resources>  
        <x:Array x:Key="ThisWorks" Type="{x:Type sys:String}">  
            <sys:String>John</sys:String>  
            <sys:String>Andy</sys:String>  
        </x:Array>  
    </Window.Resources>

我还可以在根控件的资源中实例化一个对象:

    <Window.Resources>  
        <local:Person x:Key="ThisAlsoWorks" Name="Michael" Age="40"/>  
    </Window.Resources>

但是如果我在根控件的资源中实例化一个对象数组,VS将不会让我构建:

    <Window.Resources>  
        <x:Array x:Key="ThisWontBuild" Type="{x:Type local:Person}">  
            <local:Person Name="Michael" Age="40"/>  
            <local:Person Name="Jim" Age="30"/>   
        </x:Array>
    </Window.Resources>

如果我在子控件(如网格)的资源中实例化对象数组,那么将构建VS:

    <Grid.Resources>  
        <x:Array x:Key="ThisWillBuild" Type="{x:Type local:Person}">  
            <local:Person Name="Michael" Age="40"/>  
            <local:Person Name="Jim" Age="30"/>   
        </x:Array>
    </Grid.Resources>

有人知道为什么吗?

5 个答案:

答案 0 :(得分:0)

也许你在根元素上缺少相应的xmlns

我想不出为什么它不起作用的任何其他原因,我可以告诉你,我当然可以这样做。

答案 1 :(得分:0)

使用sys而不是x作为Array.hope的xmlns,这将有所帮助。

答案 2 :(得分:0)

正如我所说的问题是我的代码,但答案比我想象的更奇怪。我的代码隐藏中有另一个类,它的唯一属性返回一个List:

public class CreateList
{
    public CreateList()
    {
        _createdList.Add("carrot");
        _createdList.Add("fox");
        _createdList.Add("explorer");
    }

    List<string> _createdList = new List<string>();

    public List<string> CreatedList
    {
        get { return _createdList; }
    }
}

如果我有一个指向数组的资源以及指向根窗口元素中的CreateList类的资源(按此顺序),则VS构建没有任何问题。因此,使用我在问题中描述的窗口根元素标题可以工作:

<Window.Resources>
    <x:Array x:Key="Office" Type="{x:Type local:Person}">
        <local:Person Name="Michael" Age="40"/>
        <local:Person Name="Jim" Age="30"/>
        <local:Person Name="Dwight" Age="30"/>
    </x:Array>
    <local:CreateList x:Key="myCreateList"/>       
</Window.Resources>

但是,如果我首先将资源指向CreateList,则VS不会构建!!!

<Window.Resources>
    <local:CreateList x:Key="myCreateList"/>
    <x:Array x:Key="Office" Type="{x:Type local:Person}">
        <local:Person Name="Michael" Age="40"/>
        <local:Person Name="Jim" Age="30"/>
        <local:Person Name="Dwight" Age="30"/>
    </x:Array>
</Window.Resources>

如果我有一个字符串数组,则会出现同样的问题,但如果我使用单个字符串或Person对象则不会。怪啊?所以我已回答了我原来的问题,但现在我有了一个新问题!典型

答案 3 :(得分:0)

我似乎有类似的问题。如果x:Array在我的UserControl上被声明为资源,我得到了编译器错误。我发现我能够将x:Array的声明移动到UserControl中的DockPanel上,然后一切正常。

答案 4 :(得分:0)

你必须创建Person类。

namespace WpfPractice
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
    }
    public class Person
    {
        public string Name { get; set; }
        public int Age { get; set; }
    }
}

参考 C# wpf Can't create an array of objects