Zune HD - Form Class
First, I wanted to talk about the Form class. This class is used to host all the Zune HD Forms controls. It is similar to the Form class that is provided in the Windows desktop development environment, for those familiar with that.
Creating a Reference to Zune.Forms.dll
If you have not downloaded the Zune.Forms.dll and referenced it in your project or the source code and set a project reference, you should do this now. The Zune.Forms.dll and source code are available on CodePlex.
Once referenced, put this line along with the others at the top of the Game1.cs file.
using Zune.Forms;
What About My Game1.cs File?
The Game1.cs file is created when you create a new project. For Zune HD Forms to function properly, we need to make a minor change to this file. Note, that in the Game1.cs file, there is a line that looks like this:
public classGame1 : Game
This class inherits from Microsft.Xna.Framework.Game class, and is required for your Zune HD application to run. In to utelize the Zune HD Forms controls, you must change this line to this:
public classGame1 : Form
At this point, you are ready to begin using controls within the Zune.Forms namespace. I am not going to go into detail on every control at this time (that will come later), but here is an example of creating a TextBox:
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
// Frame rate is 30 fps by default for Zune.
TargetElapsedTime = TimeSpan.FromSeconds(1 / 30.0);
TextBox tbFirstName = new TextBox(this);
tbFirstName.Location = new Vector2(10, 10);
tbFirstName.Size = new Vector2(100, 25);
tbFirstName.Text = "First Name:";
}
That’s it! This code will display a TextBox control on your Form.
How Does the Form Class Work?
The Form class does the heavy lifting. As shown above, the code to create a new control is simple. How it actually draws and receives user inputs is another story. Let’s take a look at some of the work the Form class does.
First, note that the Form class inherits from the Game class.
public class Form : Game
The game class contains some methods that we can override and use to our advantage. First, is the LoadContent method. This method is responsible for loading things like images and fonts. Here is how it looks in the Form class:
protected override void LoadContent()
{
_spriteBatch = new SpriteBatch(GraphicsDevice);
foreach (Control ctl in _controls)
ctl.LoadContent();
foreach (Control ctl in _sysControls)
ctl.LoadContent();
_alphaBackground = Drawing.CreateRectangle(GraphicsDevice, 272, 480, new Color(Color.LightGray, 175))
}
The important thing to note is this line:
foreach (Control ctl in _controls)
ctl.LoadContent();
This code loops through each Control in the Form’s Control collection, and calls the LoadContent() method. This allows each control to have its own content that can be loaded.
This same concept is carried over into other overridable methods like the Draw and Update methods.
This explains the Form control at a high level. I will add additional information about this control, and others in the future.