Export (0) Print
Expand All

Integrating Advertisements into Windows Phone 7 XNA Games

The following steps are a guide for using the Microsoft Advertising SDK for Windows Phone 7 to integrate advertising into Windows Phone 7 XNA games.

Prerequisites

  1. Microsoft Visual Studio 2010 (or Microsoft Visual Studio 2010 Express)

  2. Windows Phone 7 Developer Tools

  3. Microsoft Advertising Ad Control SDK for Windows Phone 7

  4. See Creating a Windows Phone Game or Library Project for the steps that must be completed to create a Windows Phone Game (4.0).

    Gg491920.Important(en-US,MSADS.10).gifImportant
    The purpose of each code sample provided in this documentation is to illustrate a concept and related syntax. Samples do not include all the code that is found in a full production system. For example, data validation and error handling code is not included. Technical support is not available for these samples or for any included source code.

Integrate an Ad into a Windows Phone 7 XNA Game

  1. In Visual Studio Solution Explorer, right-click References under your project and click Add Reference….

    XNA Add Reference Context Menu
  2. In the Add Reference dialog box, click the Browse tab. Navigate to the location where you installed the Microsoft.Advertising.Mobile.Xna.dll previously. Click Microsoft.Advertising.Mobile.Xna.dll to select it, and click OK.

    XNA Add Reference Dialog Box
  3. In the Solution Explorer panel, you will see Microsoft.Advertising.Mobile.Xna.dll listed in the References folder of your project.

    Solution Explorer Showing References
  4. Open the Game1.cs source code file, and add the following code in the using section of Game1 class code.

    using Microsoft.Advertising.Mobile.Xna;

  5. In the class variable definitions of the Game1 class, add the variables that will store the game’s AdManager object and the Ad object. To begin, find the following code that was automatically generated as part of the Game1 class.

        public class Game1 : Microsoft.Xna.Framework.Game
        {
            GraphicsDeviceManager graphics;
            SpriteBatch spriteBatch;
    
    

    After the line of code SpriteBatch spriteBatch;, add the following lines of code to declare the adManager and bannerAd variables that will hold the AdManager object and the Ad object.

            AdManager adManager;
            Ad bannerAd;
    
    

    The global class variable declarations of the Game1() class will read as follows:

        public class Game1 : Microsoft.Xna.Framework.Game
        {
            GraphicsDeviceManager graphics;
            SpriteBatch spriteBatch;
            AdManager adManager;
            Ad bannerAd;
    
    
  6. In your code, create an instance of an AdManager object and add it to the game’s Component object. To begin, find the following code that was automatically generated as part of the Game1 class in the default constructor.

            public Game1()
            {
                graphics = new GraphicsDeviceManager(this);
                Content.RootDirectory = "Content";
    
                // Frame rate is 30 fps by default for Windows Phone.
                TargetElapsedTime = TimeSpan.FromTicks(333333);
            }
    
    

    After the line of code Content.RootDirectory = "Content";, add the following lines of code to create an AdManager object.

           // Create an ad manager for this game
                adManager = new AdManager(this, "test_client");
                adManager.TestMode = true;
                Components.Add(adManager);
    
    

    The default constructor of the game, Game1(), will read as follows:

            public Game1()
            {
                graphics = new GraphicsDeviceManager(this);
                Content.RootDirectory = "Content";
    
                // Create an ad manager for this game
                adManager = new AdManager(this, "test_client");
                adManager.TestMode = true;
                Components.Add(adManager);
    
                // Frame rate is 30 fps by default for Windows Phone.
                TargetElapsedTime = TimeSpan.FromTicks(333333);
            }
    
    
  7. In your code, initialize the Ad object by using the AdManager object to create the Ad object. To begin, find the following code that was automatically generated in the Game1.LoadContent method.

            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
    
    

    Replace the comment // TODO: use this.Content to load your game content here with the following line of code to create an Ad object.

                // Create a banner ad for the game.
                bannerAd = adManager.CreateAd("Image300_50", new Rectangle(0, 20, GraphicsDevice.Viewport.Bounds.Width, 120), RotationMode.Manual, false);
    
    

    The Game1.LoadContent method will read as follows:

            protected override void LoadContent()
            {
                // Create a new SpriteBatch, which can be used to draw textures.
                spriteBatch = new SpriteBatch(GraphicsDevice);
    
                // Create a banner ad for the game.
                bannerAd = adManager.CreateAd("Image300_50", new Rectangle(0, 20, GraphicsDevice.Viewport.Bounds.Width, 120), RotationMode.Manual, false);
            }
    
    
  8. Run your game and you will see a sample test ad.

    XNA Ad Displayed in Windows Phone 7

See Also

Community Additions

Show:
© 2014 Microsoft