Export (0) Print
Expand All

How To: Get a StorageDevice Asynchronously

Demonstrates how to call BeginShowStorageDeviceSelector and EndShowStorageDeviceSelector to get a StorageDevice object asynchronously.

The Complete Sample

The code in the topic shows you the technique. 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 user needs to access the StorageDevice for the first time (for example, to save a game), call Guide.BeginShowStorageDeviceSelector, specifying which player has requested the save.

  3. Check Guide.IsVisible first to make sure the Guide is not already being displayed.

    Calling Guide.BeginShowStorageDeviceSelector when the Guide is visible results in a GuideAlreadyVisibleException. Guide.BeginShowStorageDeviceSelector 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 user selected the storage device.

  6. When IsCompleted is true, call Guide.EndShowStorageDeviceSelector, passing the IAsyncResult provided by Guide.BeginShowStorageDeviceSelector.

    The return value is the selected storage device.

  7. Use IsConnected to ensure that a valid device was chosen (if the user declines to select a device, or the device is removed, IsConnected will be 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 = Guide.BeginShowStorageDeviceSelector(PlayerIndex.One, null, null);
            }
        }
        // If a save is pending, save as soon as the
        // storage device is chosen
        if ((GameSaveRequested) && (result.IsCompleted))
        {
            StorageDevice device = Guide.EndShowStorageDeviceSelector(result);
            if (device.IsConnected)
            {
                DoSaveGame(device);
            }
            // Reset the request flag
            GameSaveRequested = false;
        }
        base.Update(gameTime);
    }
    

To get a StorageDevice asynchronously using an AsyncCallback

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

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

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

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

    You may optionally pass a tracking object to identify the request (or null).

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

  5. Call Guide.BeginShowStorageDeviceSelector when the guide is visible results in a GuideAlreadyVisibleException.

  6. In your callback method, call Guide.EndShowStorageDeviceSelector, passing the same IAsyncResult that was passed into the callback method.

    The return value is the selected storage device.

  7. Use IsConnected to ensure that a valid device was chosen (if the user declines to select a device, or the device is removed, IsConnected will be 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";
                Guide.BeginShowStorageDeviceSelector(
                                PlayerIndex.One, this.GetDevice, stateobj);
            }
        }
        base.Update(gameTime);
    }
    
    StorageDevice device;
    void GetDevice(IAsyncResult result)
    {
        device = Guide.EndShowStorageDeviceSelector(result);
        if (device.IsConnected)
        {
            DoSaveGame(device);
        }
    }
    

Community Additions

ADD
Show:
© 2014 Microsoft