示範如何以無訊息方式取得預設使用者的範例
遊戲需要建立初始使用者。此範例示範如何盡早 完成這項工作。如果您的遊戲選單想要以能清楚識別使用者的內容來「問候」使用者, 而且您不想立即顯示 UI,通常會想要遵循此模式。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;
}
示範如何取得初始使用者的範例
與上一個範例相同,遊戲需要建立初始使用者。在此情況下, 遊戲願意在要求使用者時顯示 UI。只有在系統無法判斷預設使用者時, 才會顯示 UI。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;
}
