Exemple montrant comment obtenir silencieusement l’utilisateur par défaut
Un jeu doit établir un utilisateur initial. Cet exemple montre comment y parvenir le plus tôt
possible. Vous suivrez généralement ce modèle si les menus de votre jeu doivent
« accueillir » l’utilisateur avec un élément l’identifiant clairement, et si vous ne voulez pas
afficher immédiatement l’interface utilisateur.
HRESULT Identity_TrySignInDefaultUserSilently(_In_ XTaskQueueHandle asyncQueue){ XAsyncBlock* asyncBlock = new XAsyncBlock(); asyncBlock->queue = asyncQueue; asyncBlock->callback = Identity_TrySignInDefaultUserSilently_Callback; // Request to silently sign in the default user. HRESULT hr = XUserAddAsync(XUserAddOptions::AddDefaultUserSilently, asyncBlock); if (FAILED(hr)) { delete asyncBlock; } return hr;}void CALLBACK Identity_TrySignInDefaultUserSilently_Callback(_In_ XAsyncBlock* asyncBlock){ // NOTE: XUserAddResult will add a Ref to the passed-in XUserHandle. XUserHandle newUser = nullptr; HRESULT hr = XUserAddResult(asyncBlock, &newUser); if (SUCCEEDED(hr)) { // TO DO: create Xbox services context and do something with that user. // ... } else { // You are here because there's no default user or because of an error. // Display some UI, such as "Press A or Enter to continue," and try the sign-in with the UI flow. } delete asyncBlock;}
Exemple montrant comment obtenir un utilisateur initial
Comme dans l’exemple précédent, le jeu doit établir un utilisateur initial. Dans ce cas,
le jeu accepte qu’une interface utilisateur s’affiche au moment où il demande un utilisateur. L’interface utilisateur
ne s’affiche que si le système ne peut pas déterminer l’utilisateur par défaut.
HRESULT Identity_TrySignInDefaultUser(_In_ XTaskQueueHandle asyncQueue){ XAsyncBlock* asyncBlock = new XAsyncBlock(); asyncBlock->queue = asyncQueue; asyncBlock->callback = Identity_TrySignInDefaultUser_Callback; // Request to silently sign in the default user. HRESULT hr = XUserAddAsync(XUserAddOptions::AddDefaultUserAllowingUI, asyncBlock); if (FAILED(hr)) { delete asyncBlock; } return hr;}void CALLBACK Identity_TrySignInDefaultUser_Callback(_In_ XAsyncBlock* asyncBlock){ // NOTE: XUserAddResult will add a Ref to the passed-in XUserHandle. XUserHandle newUser = nullptr; HRESULT hr = XUserAddResult(asyncBlock, &newUser); if (SUCCEEDED(hr)) { // TO DO: create Xbox services context and do something with that user. // ... } else { // You are probably here because someone canceled out of the UI flow to // sign in or an expected error occurred. // Display some UI, such as "Press A or Enter to continue," and try the sign-in // again with the UI flow. } delete asyncBlock;}