演示如何静默获取默认用户的示例
游戏需要建立一个初始用户。此示例展示了如何尽可能早地 完成这一步。如果你希望游戏菜单以清晰标识用户的信息 “问候”该用户,并且不希望立即 显示 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;
}
