Blue Theme Orange Theme Green Theme Red Theme
 
6 Months Free & No Setup Fees ASP.NET Hosting!
Home | Forums | Videos | Advertise | Certifications | Downloads | Blogs | Interviews | Jobs | Beginners | Training
 | Consulting  
Submit an Article Submit a Blog 
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
Nevron Chart
Search :       Advanced Search »
Home » Windows Phone » Building your first Windows Phone 7 Application Using XNA 4.0

Building your first Windows Phone 7 Application Using XNA 4.0

This article will be all about building a Windows Phone 7 App using XNA 4.0.

Author Rank :
Page Views : 4447
Downloads : 0
Rating :
 Rate it
Level : Beginner
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
 
Nevron Chart
Become a Sponsor
 Tag Cloud
 Latest Jobs
More ... 
 Latest Interview Questions
More ... 


This article will be all about building a Windows Phone 7 App using XNA 4.0.

This article will be a demonstration of Windows Phone 7.

So XNA 4.0 Beta is out and everything is going well. But what are the new and removed objects in XNA 4.0 Beta?

Classes, interfaces, enums added in BETA:
  • DisplayOrientation
  • AvatarRendererState
  • IAvatarAnimation
  • LaunchParameters
  • GameUpdateRequiredException
  • NetworkException(Moved from Framework.Net to Framework.GamerServices)
  • Added Framework.GamerServices namespace to Framework.Net namespace
Removed in BETA:
  • GamerServiceType
I have written an article about How To Use LaunchParameters maybe you can read it sometime. So shall we not break our topic and go on what we are going to discuss.

Lets create a Windows Phone 7 Application using WP7 Emulator (device not yet released!)

1.gif
 
It will automatically add the following code:

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 WP7App
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        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);
            // 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 should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);
            // TODO: Add your drawing code here
            base.Draw(gameTime);
        }
    }
}

There is something new for Windows Game Projects which is:

TargetElapsedTime = TimeSpan.FromTicks(333333);

This means 30 FPS is the default FPS count of WP7 Games.

And the other one is (of course) Touch namespace:

using Microsoft.Xna.Framework.Input.Touch;

Lets add some Font and display the Width and Height of the WP7.

2.gif
 
And the xml file will look like this:

<?xml version="1.0" encoding="utf-8"?>
<XnaContent xmlns:Graphics="Microsoft.Xna.Framework.Content.Pipeline.Graphics">
  <Asset Type="Graphics:FontDescription">  
    <FontName>Kootenay</FontName>
    <Size>14</Size>
    <Spacing>0</Spacing>
    <UseKerning>true</UseKerning>
    <Style>Regular</Style>
    <CharacterRegions>
      <CharacterRegion>
        <Start>&#32;</Start>
        <End>&#126;</End>
      </CharacterRegion>
    </CharacterRegions>
  </Asset>
</XnaContent>

You can set the font's properties here

Add these 2 variables:

SpriteFont Font1;
Vector2 FontPos;

SpriteFont defines the Font used for the app.Pos; it stores the position of the font.

Add these in LoadContent()

Font1 = Content.Load<SpriteFont>("MyFont");
FontPos = new Vector2(graphics.GraphicsDevice.Viewport.Width / 2, graphics.GraphicsDevice.Viewport.Height / 2);

It initializes Font object which we created. And loads its asset.

We're centering the FontPos so that the Text we will be centered.

Add these codes in Draw Function:

spriteBatch.Begin();
string output = "Height: " + graphics.PreferredBackBufferHeight + " Width: " + graphics.PreferredBackBufferWidth;
Vector2 FontOrigin = Font1.MeasureString(output) / 2;
spriteBatch.DrawString(Font1, output, FontPos, Color.Black,
0, FontOrigin, 1.0f, SpriteEffects.None, 0.5f);
spriteBatch.End();

And run the project:

3.gif
 
As you can see this is the default DisplayOrientation of WP7 Emulator.

4.gif
 
The Rotating icons are the helpers that can help you change the DisplayOrientation.

Topmost icon is Close,
The icon below is minimizer.

The 4-Arrow icon helps you to zoom-in and zoom-out.

5.gif
 
Here is how it appears...

6.gif 

The dotted-icon helps you move the WP7 emulator to anywhere you want.

7.gif 

The Key-like icon helps you open the Settings Dialog Window in which you can set Zoom Level:

8.gif 

This is it! This was your first WP7 application and in addition now you know how to use the emulator. Hope this demonstration helped you!

Comment Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post Here.
Login to add your contents and source code to this article
 [Top] Rate this article
 
 About the author
 
Ibrahim Ersoy
Ibrahim Ersoy lives in Turkey, Istanbul. He helps running and managing C# Corner. He is a Software Consultant (Sharepoint) and MindCracker MVP. He writes about cutting-edge Microsoft Technologies including XNA, Sharepoint, LINQ, SQL Server and Windows Phone 7 Development besides lately he is interested in iOS,Android and BlackBerry development.
Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Discover the Top 5 .NET Memory Management Fundamentals
To write the best .NET code, you need to know exactly how the .NET framework really manages memory. Ricky Leeks presents the Top 5 fundamental facts of .NET memory management. Learn more.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites – Click Here!
 
 Post a Feedback, Comment, or Question about this article
Subject:
Comment:
Nevron Chart
Become a Sponsor
 Comments
Nice by Mahesh On October 28, 2010
Nice
Reply | Email | Modify 
Nevron Chart
 © 2012  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.