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

 

 

 

 

 

 

 

 

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

Leave a Reply

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