Paul Rathbone
Paul Rathbone

Reputation: 55

C# wpf Can't create an array of objects

I'm trying to recreate 10. Instantiate a class in XAML

Here's the Xaml snippet from the example: -

   <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>

Here's the code-behind snippet: -

 public class Person {
        public string Name { get; set; }
        public int Age { get; set; }
    }

I've started to create a Visual Studio C# WPF 2015 project with this Xaml:-

<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>

and this code-behind: -

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; }
        }
    }
}

I'm getting an error from Type="{x:Type local:Person}" "The name "Person" does not exist in the namespace "clr-namespace:AdvFlow1".

I'm fairly newbie but already have no hair left.

Thanks, Paul.

Upvotes: 0

Views: 1647

Answers (4)

Paul Rathbone
Paul Rathbone

Reputation: 55

Thanks to @user2184057 and everyone else for their comments. This fixed it: -

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; }
    }
}

Upvotes: 0

D J
D J

Reputation: 7028

Dont make Person as a nested class. Nested class can only be used in Type

 <x:Array x:Key="Office" Type="{x:Type local:MainWindow+Person}">

But to create an instance you must take it out.

namespace AdvFlow1
{
    public class Person
    {
        public string Name { get; set; }
        public int Age { get; set; }
    }

    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
    }
}

Upvotes: 2

JaB
JaB

Reputation: 461

I would advise you to add another class. I personaly like it more to add such things in coding than in XAML.

public class Person
{
    string Name { get; set; }
    int Age { get; set; }
    public Person(string name, int age)
    {
        Name = name;
        Age = age;
    }
}

and add lines like

 public void Initialize_Persons()
 {
   new Person("John Doe", 34);
   //...
 }

Hope it helps you with youre Problem.

Upvotes: 0

DevNewb
DevNewb

Reputation: 861

In your code Person is actually nested type inside MainWindow class. I suggest moving Person to be actually inside namespace block.

Upvotes: 5

Related Questions