Exemplo que demonstra como obter silenciosamente o usuário padrão
Um jogo precisa estabelecer um usuário inicial. Este exemplo mostra como fazer isso o mais
cedo possível. Normalmente, você seguiria esse padrão se os menus do seu jogo quisessem
“cumprimentar” o usuário com algo que o identificasse claramente e você não quisesse
exibir a interface do usuário imediatamente.
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;}
Exemplo que demonstra como obter um usuário inicial
Assim como no exemplo anterior, o jogo precisa estabelecer um usuário inicial. Neste caso,
o jogo aceita que a interface do usuário seja exibida no momento em que solicita um usuário. A interface do usuário só
é exibida se o sistema não conseguir determinar o usuário padrão.
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;}