Lesson 03.5: Displaying the Player Object

In this lesson, we will connect the View (MainWindow.xaml) to the ViewModel (GameSession.cs), and display properties from the model (Player.cs).

 

 

Summary

In this lesson:

  • To let the “WPFUI” project see classes in the “Engine” project, we need to “add a reference” to the “Engine” project.
    • In the WPFUI project, right-click on the “References” and select “Add Reference…”
    • We want to add a reference to the “Engine” project, inside the solution, so we highlight “Projects” -> “Solution” and see a list of the available projects in the solution.
    • Check the “Engine” project, and the “OK” button.
    • Now, the WPFUI project has a reference to (can see the classes in) the Engine project.
  • We defined the layout of the screen in MainWindow.xaml.
    • The XAML file has a related file, where we can put C# code.
      • This is sometimes called the “code-behind” page.
      • xaml.cs is the code-behind page for MainWindow.xaml.
    • xaml.cs is another “class” – just like Player.cs, but it is for creating a screen, not a Player.
      • It has a “constructor”. This is what runs when you create a MainWindow object.
      • The constructor in MainWindow.xaml.cs runs the function “InitializeComponent()”. We will talk about functions later, but this is what draws the screen, and the objects on it (the grid, and labels).
      • We will create our Player object inside the MainWindow constructor.
    • Inside the constructor’s curly braces {}, we will instantiate a GameSession object.
      • This object will be the “DataContext” for the view – the object that will be used in the UI.

 

Source Code

GameSession.cs

 

MainWindow.xaml

 

MainWindow.xaml.cs

 

Return to main page

 

 

 

 

 

 

 

 

16 thoughts on “Lesson 03.5: Displaying the Player Object

  1. Hey, Scott. I really love all the free, helpful information you’ve provided on your site! I’m looking forward to your next lessons. I’m extremely interested in programming, enjoying C# a lot so far. I want to learn more. Do you have any tips, resources (like books), or anything else that might be useful for me? I want to focus on game programming because that’s what I want to do as a career but anything you think is good in general is highly appreciated! Thanks so much.

    1. Hello,

      I’m glad to hear you’re enjoying the lessons.

      If you want to build games, especially with better graphics, you’ll probably want to look at Unity 3D. It’s like a version of Visual Studio, and the C# framework, with additional features to make it easier to create games. Some people even use it to create professional games. I don’t know any good books for it. You might want to look at the Quill18Creates channel on YouTube. He uses Unity and has at least one game on GitHub, so you can view the source code.

      For general C# programming, the three most-recommended books are “The C# Programming Yellow Book” by Rob Miles (for general learning), “Essential C# 6.0” by Eric Lippert and Mark Michaelis, and “C# in Depth, 3rd Edition” by Jon Skeet. I’d say the first one is more of a “guidebook”, and the last two are more like “reference” books.

      1. Thanks! I’ll definitely look at those books. I’ve actually been reading the C# Yellow Book but I just felt like I wasn’t getting enough programming practice and have been going to other outlets and have ended up not reading the book much and in very short bursts.

  2. Visual studio said that the CurrentPlayer = new Player() with all is call like CurrentPlayer.Name = “Blah”; , etc… could be simplified like this:

    CurrentPlayer = new Player()
    {
    Name = “Trevis”,
    CharacterClass = “Fighter”,
    HitPoints = 10,
    Gold = 1000000,
    ExperiencePoints = 0,
    Level = 1
    };

    Are you going to use this format later? or should I keep it the how you have it for now and worry about simplification later?

  3. Hi Scott,

    I am using VS2017 and even though I have everything same as you (I even copied your source code just to be sure) and the values are not showing even though I am not getting any errors so the binding should be done properly and I was even getting the placeholder values

  4. Isn’t it a bit redundant to have a property for player level? Player level is determined by experience anyway, right? It seems like something that could get out of sync somehow and display the wrong values, but maybe I’m wrong.

  5. For anyone having the issue where the player data is not being displayed on the screen, and the program is not hitting an error.

    The cause for me was that I missed the step of making the player object public in the Gamesession Class.

    I had to change:

    in GameSession.cs to:

    This allowed access to the player information so it could be displayed.

     

    1. You definitely need to pay attention to the scope (public, internal, and private). This is especially true when creating classes. By default, Visual Studio creates them without an explicit scope. So, they default to “internal” (only visible to other classes in the same project). For many of our classes, they need to be set to “public”.

  6. Great video and lesson. I have XAML with values from code! I have resisted WPF for a while but your lessons are really great!

  7. Hi all,
    I have a problem with using Engine.ViewModels in MainWindow.xaml.cs i am getting error Namespace is not recognized, but reference is there. I copied your code and error is still there. I have Visual Studio 2017 and NET 3.5
    where can be problem please?
    Thank you

    1. Hello Michal,

      When you created the Engine project, did it have a different name, and did you rename it? If that happened, you might need to fix the namespace for the project. Here is a link to the Windows Form version of the project that shows how to fix that: http://scottlilly.com/learn-c-by-building-a-simple-rpg-index/lesson-02-2b-fixing-the-namespace/.

      If that is not the problem, can you upload your solution (including the directories under it, and all the files in those directories) to GitHub or Dropbox, so I can look at it?

      1. Hi Scott,
        Thanks for reply, i already solved it, it was my bad i somehow forget set that reference in project properties. Thank you again 🙂 And when will be next lesson? Im looking forward to save/load chapter and complex combat system, btw good job with this tutorial, i like it, keep it up.

        1. You’re welcome. I was working on the next lesson this morning. It should be up Thursday or Friday. It’s a small one, with more keyboard input. I’m trying to figure out what to work on after that. I think it will be converting the factories to read from XML files, so everyone can create their own custom game.

Leave a Reply

Your email address will not be published. Required fields are marked *