1. Hello Xna for Windows Phone 7
2. xna Code Execution Process
3. Download Code
1. Hello Xna for Windows Phone 7
1.1 create an xna project.
Vs will generate the following project.
The XnaHelloPhoneContent project contains the resource files (bitmap file, effect file, sprite file) to be used in the project, and the XnaHelloPhone project contains references to the XnaHelloPhoneContent project, you can directly use these resources in the Code. For example, if you want to load font resources, the Code is as follows:
Font = this. Content. Load <SpriteFont> ("SpriteFont1 ");
The parameter in the Load method is the asset name of the font.
1.2 modify the code.
Game1.cs:
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using Microsoft. Xna. Framework;
Using Microsoft. Xna. Framework. Audio;
Using Microsoft. Xna. Framework. Content;
Using Microsoft. Xna. Framework. GamerServices;
Using Microsoft. Xna. Framework. Graphics;
Using Microsoft. Xna. Framework. Input;
Using Microsoft. Xna. Framework. Input. Touch;
Using Microsoft. Xna. Framework. Media;
Namespace XnaHelloPhone
{
/// <Summary>
/// This is the main type for your game
/// </Summary>
Public class Game1: Microsoft. Xna. Framework. Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
// Displayed text
Private string text = "Hello Window Phone 7! ";
// Text font
Private SpriteFont font;
// Text position
Private Vector2 textPosition;
Public Game1 ()
{
Graphics = new GraphicsDeviceManager (this );
Content. RootDirectory = "Content ";
// Frame rate is 30 fps by default for Windows Phone.
TargetElapsedTime = TimeSpan. FromTicks (333333 );
}
/// <Summary>
/// Allows the game to perform any initialization it needs to before starting to run.
/// This is where it can query for any required services and load any non-graphic
/// Related content. Calling base. Initialize will enumerate through any components
/// And initialize them as well.
/// </Summary>
Protected override void Initialize ()
{
// TODO: Add your initialization logic here
Base. Initialize ();
}
/// <Summary>
/// LoadContent will be called once per game and is the place to load
/// All of your content.
/// </Summary>
Protected override void LoadContent ()
{
// Create a new SpriteBatch, which can be used to draw textures.
SpriteBatch = new SpriteBatch (GraphicsDevice );
Font = this. Content. Load <SpriteFont> ("SpriteFont1 ");
Vector2 textSize = font. MeasureString (text );
Viewport viewpoint = this. GraphicsDevice. Viewport;
This. textPosition = new Vector2 (viewpoint. Width-textSize. X)/2,
(Viewpoint. Height-textSize. Y)/2 );
// TODO: use this. Content to load your game content here
}
/// <Summary>
/// UnloadContent will be called once per game and is the place to unload
/// All content.
/// </Summary>
Protected override void UnloadContent ()
{
// TODO: Unload any non ContentManager content here
}
/// <Summary>
/// Allows the game to run logic such as updating the world,
/// Checking for collisions, gathering input, and playing audio.
/// </Summary>
/// <Param name = "gameTime"> Provides a snapshot of timing values. </param>
Protected override void Update (GameTime gameTime)
{
// Allows the game to exit
If (GamePad. GetState (PlayerIndex. One). Buttons. Back = ButtonState. Pressed)
This. Exit ();
// TODO: Add your update logic here
Base. Update (gameTime );
}
/// <Summary>
/// This is called when the game shoshould draw itself.
/// </Summary>
/// <Param name = "gameTime"> Provides a snapshot of timing values. </param>
Protected override void Draw (GameTime gameTime)
{
GraphicsDevice. Clear (Color. Navy );
// TODO: Add your drawing code here
SpriteBatch. Begin ();
SpriteBatch. DrawString (font, this. text,
TextPosition,
Color. White );
SpriteBatch. End ();
Base. Draw (gameTime );
}
}
}
1.3 run the program
2. xna Code Execution Process
3. Download Code
/Files/xuqiang/XnaHelloPhone.rar