How to use the game invite task for Windows Phone 8
[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]
Use the game invite task to show a game invite screen that enables the user to invite players to a multiplayer game session. The invitation is sent asynchronously.
By using Choosers, you help provide a consistent user experience throughout the Windows Phone platform. For more information, see Launchers and Choosers for Windows Phone 8.
Warning
The game invite task works only when called from within a game that is approved for release on Xbox LIVE on Windows Phone. For more information, see Developing Games for Windows Phone 7.
To use the game invite task
Add the following statement to your code.
using Microsoft.Phone.Tasks;
Imports Microsoft.Phone.Tasks
Declare the task object. It must have page scope, so declare it in your page before the constructor.
GameInviteTask gameInviteTask;
Dim gameInviteTask As GameInviteTask
Add the following code to your page constructor. This code initializes the task object, and identifies the method to run after the user completes the task.
gameInviteTask = new GameInviteTask(); gameInviteTask.Completed += new EventHandler<TaskEventArgs>(gameInviteTask_Completed);
gameInviteTask = new GameInviteTask() AddHandler gameInviteTask.Completed, AddressOf gameInviteTask_Completed
Add the following code to your application wherever you need it, such as in a button click event. To test this procedure, you can put the code in the page constructor. This is the code to launch the task.
gameInviteTask.SessionId = "<my session id>"; gameInviteTask.Show();
gameInviteTask.SessionId = "<my session id>" gameInviteTask.Show()
Add the code for the completed event handler to your page. This code runs after the user completes the task. You can check to see whether the invite was sent successfully.
void gameInviteTask_Completed(object sender, TaskEventArgs e) { switch(e.TaskResult) { //Game logic for when the invite was sent successfully case TaskResult.OK: MessageBox.Show("Game invitation sent."); break; //Game logic for when the invite is cancelled by the user case TaskResult.Cancel: MessageBox.Show("Game invitation cancelled."); break; // Game logic for when the invite could not be sent case TaskResult.None: MessageBox.Show("Game invitation could not be sent."); break; } }
Private Sub gameInviteTask_Completed(sender As Object, e As TaskEventArgs) Select Case e.TaskResult 'Game logic for when the invite was sent successfully Case TaskResult.OK MessageBox.Show("Game invitation sent.") 'Game logic for when the invite is cancelled by the user Case TaskResult.Cancel MessageBox.Show("Game invitation cancelled.") 'Game logic for when the invite could not be sent Case TaskResult.None MessageBox.Show("Game invitation could not be sent.") End Select End Sub