2 out of 12 rated this helpful - Rate this topic

Using the XNA API

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

ImportantImportant
The purpose of the sample code in this topic is to illustrate a concept and related syntax. The sample does 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 this sample or for any included source code.

A complete working game, with code for using geo location ad targeting, ad borders, and the events AdRefreshed and ErrorOccurred is available for download at code.microsoft.com.

Prerequisites

Integrate an Ad into a Windows Phone XNA Game

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

    XNA Add Reference Context Menu
  2. In the Add Reference dialog box, click the .NET tab. Select the Microsoft.Advertising.Mobile.Xna.dll file, 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. Next you will add the variables that will store the game’s DrawableAd object. To begin, find the following variable definitions of the Game1 class. These are 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 line of code to declare the bannerAd variable that will hold the DrawableAd object.

            DrawableAd 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;
            DrawableAd bannerAd;
    
    
  6. Next, you will initialize an AdGameComponent object, and then you will add it to the game’s Component object.

    Find the Initialize method that was automatically generated. It has the following code:

            protected override void Initialize()
            {
                // TODO: Add your initialization logic here
    
                base.Initialize();
            }
    
    

    After the // TODO add the following code to initialize the AdGameComponent object. For documentation about the overloaded Initialize method, see the topic AdGameComponent.Initialize.

    
                // Initialize the AdGameComponent and add it to the game’s Component object
                AdGameComponent.Initialize(this, "test_client");
                Components.Add(AdGameComponent.Current);
    
    

    At this point, your Initialize method will look like this:

            protected override void Initialize()
            {
                // TODO: Add your initialization logic here
                
                // Initialize the AdGameComponent and add it to the game’s Component object
                AdGameComponent.Initialize(this, "test_client");
                Components.Add(AdGameComponent.Current);
    
                base.Initialize();
            }
    
    
  7. Next you will add your own CreateAd method to the Game1 class. In this Game1.CreateAd() method you will put all the code necessary to define the banner ad for your game. Later, you will write the code necessary to make a call to this method from the Game1.Initialize() method.

    Scroll down to the end of the Game1 class, which is after the Draw method. That code looks like this:

            /// <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);
            }        
    }
    
    

    After the Draw method close bracket, and before the Game1 class close bracket, add the following code for the body of your CreateAd method.

            /// <summary>
            /// Create a DrawableAd with desired properties.
            /// </summary>
            private void CreateAd()
            {
                  // Create a banner ad for the game.
    
            }
    
    
  8. Next, after the line of code, // Create a banner ad for the game, add the code that will define the dimensions of your banner ad.

                int width = 480;
                int height = 80;
                int x = (GraphicsDevice.Viewport.Bounds.Width - width) / 2; // centered on the display
                int y = 5;
    
    

    At this point, your CreateAd method looks like this.

            private void CreateAd()
            {
                // Create a banner ad for the game.
                int width = 480;
                int height = 80;
                int x = (GraphicsDevice.Viewport.Bounds.Width - width) / 2; // centered on the display
                int y = 5;
            }
    
    
  9. To complete your CreateAd method, you will create the DrawableAd object. Add the following code to the CreateAd method.

    bannerAd = AdGameComponent.Current.CreateAd(“Image480_80”, new Rectangle(x, y, width, height), true);
    

    So your complete CreateAd method will look like this.

            private void CreateAd()
            {
                // Create a banner ad for the game.
                int width = 480;
                int height = 80;
                int x = (GraphicsDevice.Viewport.Bounds.Width - width) / 2; // centered on the display
                int y = 5;
                
                bannerAd = AdGameComponent.Current.CreateAd(“Image480_80”, new Rectangle(x, y, width, height), true);
            }
    
    
  10. Now you will add a call to the CreateAd method in the Initialize method for Game1.

    Add the code CreateAd() to the Initialize method so the complete Initialize method looks like this.

            protected override void Initialize()
            {
                // Initialize the AdGameComponent with your ApplicationId and add it to the game.
                AdGameComponent.Initialize(this, “test_client”);
                Components.Add(AdGameComponent.Current);
    
                // Create an actual ad for display.
                CreateAd();
    
                base.Initialize();
            }
    
    
  11. Build and run your game and you will see a sample test ad.

    XNA Ad Displayed in Windows Phone 7

See Also


Send feedback about this documentation to adsfdbk@microsoft.com.
Publication date: 2012-03-27
Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ