Export (0) Print
Expand All

Getting a StorageDevice Asynchronously

Demonstrates how to call BeginShowSelector and StorageDevice.EndShowSelector to get a StorageDevice object asynchronously.

Complete Sample

The code in the topic shows you the technique for getting a StorageDevice asynchronously. You can download a complete code sample for this topic, including full source code and any additional supporting files required by the sample.

Getting a StorageDevice Asynchronously

To get a StorageDevice asynchronously using IsCompleted

  1. In your Game constructor, add a new GamerServicesComponent to your Components collection.

  2. Create a variable to track when a request for a StorageDevice is pending. In this case, declare GameSaveRequested.

    When the player needs to access the StorageDevice for the first time (for example, to save a game), call StorageDevice.BeginShowSelector, specifying which player requested the save.

  3. Check Guide.IsVisible first to ensure the Guide is not already displayed.

    Calling StorageDevice.BeginShowSelector when the Guide is visible results in a GuideAlreadyVisibleException. StorageDevice.BeginShowSelector returns an IAsyncResult interface that is used to determine when the asynchronous request is finished.

  4. Set your tracking variable to indicate a request is pending.

  5. When a request is pending, check IAsyncResult.IsCompleted periodically to determine when the player selected the storage device.

  6. When IsCompleted is true, call StorageDevice.EndShowSelector, passing the IAsyncResult provided by StorageDevice.BeginShowSelector.

    The return value is the selected storage device.

  7. Use the return value of EndShowSelector to ensure that a valid device is chosen.

    If the player declines to select a device, null is returned. If the device was removed, IsConnected is false.

  8. If the StorageDevice is connected, use it to load or save data.

  9. Reset your tracking variable.

    public Game1()
    {
        graphics = new GraphicsDeviceManager(this);
        Content.RootDirectory = "Content";
    
        this.Components.Add(new GamerServicesComponent(this));
    }
    IAsyncResult result;
    Object stateobj;
    bool GameSaveRequested = false;
    GamePadState currentState;
    
    protected override void Update(GameTime gameTime)
    {
        GamePadState previousState = currentState;
        currentState = GamePad.GetState(PlayerIndex.One);
        // Allows the default game to exit on Xbox 360 and Windows
        if (currentState.Buttons.Back == ButtonState.Pressed)
            this.Exit();
    
        if ((currentState.Buttons.A == ButtonState.Pressed) &&
            (previousState.Buttons.A == ButtonState.Released))
        {
            // Set the request flag
            if ((!Guide.IsVisible) && (GameSaveRequested == false))
            {
                GameSaveRequested = true;
                result = StorageDevice.BeginShowSelector(
                        PlayerIndex.One, null, null);
            }
        }
    
        // If a save is pending, save as soon as the
        // storage device is chosen
        if ((GameSaveRequested) && (result.IsCompleted))
        {
            StorageDevice device = StorageDevice.EndShowSelector(result);
            if (device != null && device.IsConnected)
            {
                DoSaveGame(device);
            }
            // Reset the request flag
            GameSaveRequested = false;
        }
        base.Update(gameTime);
    }
    

To get a StorageDevice asynchronously using AsyncCallback

  1. In your Game constructor, add a new GamerServicesComponent to your Components collection.

  2. Create an AsyncCallback object that represents the method to be called when the player chooses a device.

    That function must take an IAsyncResult as a parameter, and return void.

  3. When the player needs to access the StorageDevice for the first time (for example, to save a game), call StorageDevice.BeginShowSelector, specifying which player requested the save, and your AsyncCallback object.

    As an option, you may pass a tracking object to identify the request (or null).

  4. Check Guide.IsVisible first to ensure the guide is not already being displayed.

  5. Call StorageDevice.BeginShowSelector when the guide is visible results in a GuideAlreadyVisibleException.

  6. In your callback method, call StorageDevice.EndShowSelector, passing the same IAsyncResult passed into the callback method.

    The return value is the selected storage device.

  7. Use the return value of EndShowSelector to ensure that a valid device is chosen.

    If the player declines to select a device, null is returned. If the device was removed, IsConnected is false.

  8. If the StorageDevice is connected, use it to load or save data.

    public Game1()
    {
        graphics = new GraphicsDeviceManager(this);
        Content.RootDirectory = "Content";
    
        this.Components.Add(new GamerServicesComponent(this));
    }
    IAsyncResult result;
    Object stateobj;
    bool GameSaveRequested = false;
    GamePadState currentState;
    
    protected override void Update(GameTime gameTime)
    {
        GamePadState previousState = currentState;
        currentState = GamePad.GetState(PlayerIndex.One);
        // Allows the default game to exit on Xbox 360 and Windows
        if (currentState.Buttons.Back == ButtonState.Pressed)
            this.Exit();
    
        if ((currentState.Buttons.B == ButtonState.Pressed) &&
            (previousState.Buttons.B == ButtonState.Released))
        {
            if (!Guide.IsVisible)
            {
                // Reset the device
                device = null;                   
                stateobj = (Object)"GetDevice for Player One";
                StorageDevice.BeginShowSelector(
                        PlayerIndex.One, this.GetDevice, stateobj);
            }
        }
        base.Update(gameTime);
    }
    
    StorageDevice device;
    void GetDevice(IAsyncResult result)
    {
        device = StorageDevice.EndShowSelector(result);
        if (device != null && device.IsConnected)
        {
            DoSaveGame(device);
        }
    }
    

Community Additions

ADD
Show:
© 2014 Microsoft