XUserGetGamerPictureAsync

异步检索特定用户的玩家图片。

语法

HRESULT XUserGetGamerPictureAsync(  
         XUserHandle user,  
         XUserGamerPictureSize pictureSize,  
         XAsyncBlock* async  
)  

参数

user _In_
类型:XUserHandle

要检索其玩家图片的用户。

pictureSize _In_
类型:XUserGamerPictureSize

要检索的玩家图片的大小。

async _Inout_
类型:XAsyncBlock*

用于轮询调用的状态和检索调用结果的 XAsyncBlock

返回值

类型:HRESULT

HRESULT 成功或错误代码。
有关错误代码的列表,请参阅错误代码

备注

“显示名称和玩家图片”XR 列出了用户名和虚拟形象图像如何显示的特定要求。 请参阅“Xbox One 游戏和中心应用的 Xbox 要求”(“Xbox 开发人员下载”->“>合作伙伴、发布和发布管理信息”->“>XGD 合作伙伴文档”),了解详细信息。

要检索 XUserGetGamerPictureAsync 调用的结果,请调用 XUserGetGamerPictureResult

要检索 {XUserGetGamerPictureResult](xusergetgamerpictureresult.md) 所需的缓冲区大小以检索 XUserGetGamerPictureAsync 返回的玩家图片,请调用 XUserGetGamerPictureResultSize

返回的游戏玩家图片的格式是 .PNG 格式的图像。 图片大小最大为 2^32 字节。

下面的示例演示如何加载玩家图片。

HRESULT LoadGamerPicComplete(XAsyncBlock* abResult)
{
    try
    {
        struct CreateTextureContext
        {
            User* This;
            ImTextureID TextureId;
            std::vector<unsigned char> GamerpicBuffer;
        };

        auto context = std::make_unique<CreateTextureContext>();
        context->This = this;
        context->TextureId = IMTEXTUREID_INVALID;

        // Get the buffer size and set up a buffer to contain it
        size_t bufferSize;
        RETURN_IF_FAILED(XUserGetGamerPictureResultSize(abResult, &bufferSize));
        context->GamerpicBuffer.resize(bufferSize);

        size_t bufferUsed;
        RETURN_IF_FAILED(XUserGetGamerPictureResult(
            abResult,
            context->GamerpicBuffer.size(),
            context->GamerpicBuffer.data(),
            &bufferUsed));
        FAIL_FAST_HR_IF(E_UNEXPECTED, bufferSize != bufferUsed);

        // Create some async work to create a dx texture off the UI thread
        // and then set the texture id back on the UI thread
        auto abCreateTexture = std::make_unique<XAsyncBlock>();
        ZeroMemory(abCreateTexture.get(), sizeof(*abCreateTexture));
        abCreateTexture->queue = abResult->queue;
        abCreateTexture->context = context.get();

        // Set the texture id in the completion on the UI thread
        abCreateTexture->callback = [](XAsyncBlock* ab)
        {
            auto context = static_cast<CreateTextureContext*>(ab->context);
            context->This->_textureId = context->TextureId;
            delete context;
            delete ab;
        };

        // Worker will create the texture on a work thread
        auto createTextureWorker = [](XAsyncBlock* ab) -> HRESULT
        {
            auto context = static_cast<CreateTextureContext*>(ab->context);
            auto dimension = GetDimensionFromSize(GamerPicSize);
            RETURN_IF_FAILED(CreateTextureFromPng(context->GamerpicBuffer.data(), context->GamerpicBuffer.size(), &context->TextureId));
            return S_OK;
        };

        // Create the texture on a work thread
        if (SUCCEEDED_LOG(XAsyncRun(abCreateTexture.get(), createTextureWorker)))
        {
            context.release();
            abCreateTexture.release();
        }
    }
    CATCH_RETURN();
    return S_OK;
}

HRESULT LoadGamerPicAsync(XTaskQueueHandle queue, uint32_t cancelTime = 0)
{
    auto asyncBlock = std::make_shared<XAsyncBlock>();
    ZeroMemory(asyncBlock.get(), sizeof(*asyncBlock));
    asyncBlock->queue = queue;

    struct GamerPicContext
    {
        User* User;
        std::shared_ptr<XAsyncBlock> AsyncBlock;
    };

    auto context = std::make_unique<GamerPicContext>();
    context->User = this;
    context->AsyncBlock = asyncBlock;
    asyncBlock->context = context.get();

    asyncBlock->callback = [](XAsyncBlock* ab)
    {
        std::unique_ptr<GamerPicContext> context(reinterpret_cast<GamerPicContext*>(ab->context));
        LOG_IF_FAILED(context->User->LoadGamerPicComplete(ab));
    };

    if (cancelTime != 0)
    {
        std::thread cancelThread(
            [asyncBlock]()
        {
            std::this_thread::sleep_for(std::chrono::milliseconds(5000));
            XAsyncCancel(asyncBlock.get());
        });

        cancelThread.detach();
    }

    if (SUCCEEDED_LOG(XUserGetGamerPictureAsync(_handle.get(), GamerPicSize, asyncBlock.get())))
    {
        context.release();
    }

    return S_OK;
}

要求

头文件:XUser.h

库:xgameruntime.lib

支持平台:Windows、Xbox One 系列主机和 Xbox Series 主机

另请参阅

XUser

XUserGetGamerPictureResult

XUserGetGamerPictureResultSize