C#wpf无法创建对象数组

Pau*_*one 0 c# wpf xaml

我正在尝试重新创建10.在XAML中实例化一个类

这是示例中的Xaml片段: -

   <Grid.Resources>
        <!-- Create a array of Person objects -->
        <x:Array x:Key="Office" Type="{x:Type local:Person}">
            <!-- Instantiate a Person and add to the array -->
            <local:Person Name="Michael" Age="40"/>
            <local:Person Name="Jim" Age="30"/>
            <local:Person Name="Dwight" Age="30"/>
        </x:Array>
    </Grid.Resources>
Run Code Online (Sandbox Code Playgroud)

这是代码隐藏代码段: -

 public class Person {
        public string Name { get; set; }
        public int Age { get; set; }
    }
Run Code Online (Sandbox Code Playgroud)

我已经开始用这个Xaml创建一个Visual Studio C#WPF 2015项目: -

<Window x:Class="AdvFlow1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:AdvFlow1"
        xmlns:sys="clr-namespace:System;assembly=mscorlib"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
    <Grid.Resources>
        <!-- Create a array of Person objects -->
        <x:Array x:Key="Office" Type="{x:Type local:Person}">
            <!-- Instantiate a Person and add to the array -->
            <local:Person Name="Michael" Age="40"/>
            <local:Person Name="Jim" Age="30"/>
            <local:Person Name="Dwight" Age="30"/>
        </x:Array>
    </Grid.Resources>
    </Grid>
</Window>
Run Code Online (Sandbox Code Playgroud)

这个代码隐藏: -

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace AdvFlow1
{
    /// <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; }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我收到一个错误来自Type="{x:Type local:Person}""名称"人"在命名空间中不存在"clr-namespace:AdvFlow1".

我是相当新手但已经没有留下头发了.

谢谢,保罗.

Dev*_*ewb 5

在你的代码中,Person实际上是MainWindow类中的嵌套类型.我建议将Person移动到名称空间块中.