0

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.

2
  • 2
    Try putting Person out of MainWindowClass Commented Sep 16, 2015 at 7:17
  • That worked wonderfully, thank you. I will look for the wood as well as the trees next time. Commented Sep 16, 2015 at 7:24

4 Answers 4

5

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

Sign up to request clarification or add additional context in comments.

Comments

2

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

Comments

0

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.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.