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
-
In your Game constructor, add a new GamerServicesComponent to your Components collection.
-
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.
-
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.
-
Set your tracking variable to indicate a request is pending.
-
When a request is pending, check IAsyncResult.IsCompleted periodically to determine when the user selected the storage device.
-
When IsCompleted is true, call Guide.EndShowStorageDeviceSelector, passing the IAsyncResult provided by Guide.BeginShowStorageDeviceSelector.
The return value is the selected storage device.
-
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).
-
If the StorageDevice is connected, use it to load or save data.
-
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 != null && device.IsConnected)
{
DoSaveGame(device);
}
// Reset the request flag
GameSaveRequested = false;
}
base.Update(gameTime);
}
To get a StorageDevice asynchronously using an AsyncCallback
-
In your Game constructor, add a new GamerServicesComponent to your Components collection.
-
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.
-
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).
-
Check Guide.IsVisible first to make sure the guide is not already being displayed.
-
Call Guide.BeginShowStorageDeviceSelector when the guide is visible results in a GuideAlreadyVisibleException.
-
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.
-
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).
-
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 != null && device.IsConnected)
{
DoSaveGame(device);
}
}
Concepts
Reference