0

I'm trying to do this simple tutorial and I'm upto the second part: http://msdn.microsoft.com/en-us/library/cc265158(v=vs.95).aspx

(In my code I've just replaced Customer with Game

but I keep getting the errors: The name "Games" does not exist in the namespace

"clr-namespace:GameLauncher". Unknown type 'Games' in XML namespace 'clr-namespace:GameLauncher;assembly=GameLauncher, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'

My code for the XAML is:

<Page
x:Class="GameLauncher.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:GameLauncher"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:src="clr-namespace:GameLauncher"
mc:Ignorable="d">

<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
    <Grid.Resources>
        <src:Games x:Key="games"/>
    </Grid.Resources>
    <ListBox HorizontalAlignment="Left" Height="{Binding ElementName=LayoutRoot, Path=ActualHeight}" Margin="50,50,0,50" VerticalAlignment="Stretch" Width="300"/>
</Grid>

and my C# code is:

namespace GameLauncher
{

public class Game
{
    public String FirstName { get; set; }
    public String LastName { get; set; }
    public String Address { get; set; }

    public Game(String firstName, String lastName, String address)
    {
        this.FirstName = firstName;
        this.LastName = lastName;
        this.Address = address;
    }

}

public class Games : ObservableCollection<Game>
{
    public Games()
    {
        Add(new Game("Michael", "Anderberg",
                "12 North Third Street, Apartment 45"));
        Add(new Game("Chris", "Ashton",
                "34 West Fifth Street, Apartment 67"));
        Add(new Game("Cassie", "Hicks",
                "56 East Seventh Street, Apartment 89"));
        Add(new Game("Guido", "Pica",
                "78 South Ninth Street, Apartment 10"));
    }

}

public sealed partial class MainPage : Page
{
    public MainPage()
    {
        this.InitializeComponent();
    }

    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
    }
}
}

I'm most likely doing something stupidly wrong, I haven't coded in quite a while.

I had it worked for one second, then I when I was changing it from Customers to Games, it all stopped working and I couldn't get it working again, even if I changed it back to customers, even if I started the project from scratch again.

Vishal Suthar
  • 17,013
  • 3
  • 59
  • 105
user1940572
  • 25
  • 1
  • 5
  • Are you absolutely sure that the Games class is inside the GameLauncher namespace? – Denys Wessels Jan 01 '13 at 11:01
  • I've got all my code that I've done shown, it looks inside it to me. – user1940572 Jan 01 '13 at 13:51
  • In the tutorial they use a UserControl instead of a Page, have you tried that? If you are not going to use navigation, just use Window or UserControl (to display within a window). – Hannish Jan 03 '13 at 00:58
  • Still get the unknown type error – user1940572 Jan 03 '13 at 03:56
  • Spent a few hours trying multiple different things, still CAN'T get this working. I have no idea what I'm doing wrong, and no idea why it was working once and now isn't. – user1940572 Jan 03 '13 at 05:04
  • I just had this same error and the error was actually in the class I was trying to use (in your case Games). In my case it was an Interface that had a method signature that shouldn't have been in there (and was therefore not defined in the implementing class). – BrianKE Jan 03 '13 at 19:53
  • 1
    Is `xmlns:local="using:GameLauncher"` a valid way to define a namespace? Often you have to Rebuild in order for your XAML to recognize a class inside a namespace, and you might have an error preventing you from Rebuilding the project, so the error you're getting may be misleading. If you comment out the `` part of the XAML, can you rebuild the project? – Rachel Jan 16 '13 at 14:09
  • I think this may be a circular dependency with the XAML. I get this error all the time when I add a new class that hasn't been compiled yet and try to reference it in XAML. The XAML can't find the class in the project DLL, which actually prevents you from compiling the DLL. So you have to remove all references to the `Game` class from the XAML, compile the project so `Game` exists in the DLL, then add the tags back to the XAML and it will be happy and allow you to continue compiling your application. It's a terrible circular dependency in the compiling process as far as I can tell. – Triynko Feb 12 '14 at 21:15
  • I did some more research into this, and the problem has to do with using the Name attribute instead of the x:Name attribute on classes that are in the same assembly. You must use the x:Name attribute. Usually, they do the same thing, but when you're referencing something in XAML that's in the same assembly you're building, you need to use the x:Name attribute. See: http://stackoverflow.com/questions/1718814/ive-recently-started-learning-wpf-on-my-own-what-is-difference-when-declaring/1718826#1718826 (in particular the comment under the answer saying they're the same; they aren't). – Triynko Feb 13 '14 at 16:49

1 Answers1

0

I don't see where you are creating the Games object within your code. You will need a Games property with a getter/setter in order to use it in the XAML as you are.

Within MainPage() you need to do something like this:

public sealed partial class MainPage : Page
{
    public MainPage()
    {
        this.InitializeComponent();
        this.Games = new Games();    // this will execute the Games constructor 
                                     // and add the games to Games
    }

    // allows you to use 'Games' in your xaml
    public ObservableCollection<Game> Games  
    {
        get;
        set;
    }

    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
    }
}

As a point of clarity I would use something like 'AllGames', 'CurrentGame', etc. instead of just Games.

BrianKE
  • 4,035
  • 13
  • 65
  • 115
  • Thanks this has been the most informative answer so far, yet I still can't get it working. All my code is exactly the same as my last post, but I am including your additions. – user1940572 Jan 04 '13 at 00:13
  • I was able to get it working a different way, I added x:Name="Main" to and added ItemsSource="{Binding ElementName=Main, Path=AllGames}" to my ListBox. Is this a good way to retrieve the list? It's working. – user1940572 Jan 04 '13 at 00:27
  • It might be that your DataContext on your xaml is not set to that of your MainWindow, in which case it won – BrianKE Jan 04 '13 at 02:16
  • It might be that your DataContext on your xaml is not set to that of your MainWindow, in which case it won't .be able to find 'Games'. In your you need to set the DataContext to that of MainPage using (if memory serves correctly). A very good, simple and easy to follow example can be found here: http://rachel53461.wordpress.com/2011/05/08/simplemvvmexample – BrianKE Jan 04 '13 at 02:23