演示如何以无提示方式获取默认用户的示例
游戏需要建立初始用户。 此示例演示如何尽快地实现此操作。 如果你的游戏菜单需要向用户显示可明确其身份的问候语,并且你不希望立即显示 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;
}