Lesson 04.1: Creating the Location class

In this lesson, we will create a class to hold the Location information (including a graphic file to display).

 

 

Summary

To add an image to your project:

  • Create the image in your preferred graphics program.
  • Add the file to the project, as an existing item.
  • Set the image’s “Build Action” property to “Resource”
  • When displaying the file, you will need to include the assembly (project) name where the file is located, along with the path (folders) to the image file.
    • For this project, it will be “/Engine;component/Image/Locations/Home.png” (for the Home location image).

 

Image Files

All image files, in a zip file

Or, download the individual images (right-click, and save to your disk):

Source Code

Location.cs

 

GameSession.cs

 

MainWindow.xaml

 

Return to main page

6 thoughts on “Lesson 04.1: Creating the Location class

  1. Hi Scott,

    Just a comment – when adding the images and changing the Build Action from Content to Resource, you can select all the images in the Solution Explorer at once and change them all at once, rather than clicking each one and changing it individually.

    To select multiple items:
    1> click on the first one, and hold Shift down and click on the last one, to highlight all the items
    2> Also, individual items can be added or removed from the highlighted list by holding down Ctrl and clicking.
    3> Once the desired list of all items is selected, in the properties window, change Build Action just like you would for a single item, but it will change it for all highlighted items.

    I think this is good to point out because it can be applied to any objects in the solution that need to have their properties updated to the same thing.

    1. Yes. This is especially useful when working on Windows Forms programs, when you want to change some properties for many textboxes/labels/buttons. It makes setting height/width/alignment/etc. much faster than individually clicking on each one.

  2. Hi Scott,

    Thanks for these tutorials!

    Can you explain why the “;component” is needed after “/Engine” in the Image path?

    1. You’re welcome, Mike.

      If you want one project (WPFUI) to use a resource (image) from another project (Engine), you need to include “;component”. If the resource is in the same project/assembly, you do not need to include “;component”.

      There is some more information on this at: Microsoft documentation

  3. Hello Scott,

    Thanks for this awesome tutorial. When I tried to add the build option “Resource” on the images it wasn’t available. I found out that it was because it was in the Engine project. Once I moved it into the WPFUI project the buildAction option for “Resource” became available. I am guessing it has to do with the references or type of project my Engine project is. I noticed there were two types of class libraries to choose from in visual studio 2017 the .net framework version and the .net standard version. I choose the standard. If you know why the buildaction “resource” wasn’t available please let me know. I tried stackoverflow but came up with nothing

    Emmanuel

    1. Hello Emmanuel,

      I haven’t worked with .NET standard projects, but Microsoft has been trying to make .NET usable in different environments (Mac, Linux, etc.). However, some functions are not available in all environments – or, at least not available right now. This is especially true with .NET Core. There is some more information on this Stack Overflow question. Although, the answer might be more confusing than the question. I haven’t found anything that specifically shows why resource wouldn’t be allowed in a .NET Standard Class Library.

      In my copy of the solution, using Visual Studio Community 2017, the Engine project is a “Class Library (.NET Framework)” under Visual C# -> Windows Classic Desktop, and it allows adding an image as a resource.

Leave a Reply

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