> ## Documentation Index
> Fetch the complete documentation index at: https://devdocs.xbox.com/llms.txt
> Use this file to discover all available pages before exploring further.

# 发送邀请

> 使用 Title-Callable UI 覆盖层或 XblMultiplayerSendInvitesAsync API（配合自定义游戏内 UI）从游戏中发送 XBOX 多人游戏邀请。

使用本主题在 XBOX 服务上配置用户对用户的多人游戏会话。

您的游戏会向其他用户发送邀请以加入您的游戏会话。可通过两种方式向其他用户发送游戏邀请。
您可以使用 Title-Callable UI (TCUI)（系统 UI 覆盖层），也可以使用游戏自实现的自定义 UI。

## 使用 Title-Callable UI (TCUI)（系统 UI 覆盖层）

调用 [XGameUiShowSendGameInviteAsync](/reference/system/xgameui/functions/xgameuishowsendgameinviteasync) API 可获取用于邀请好友的标准系统 UI 覆盖层。
这会显示一个 UI，用户可在其中导航以选择好友或从[最近玩家列表](/services/xbox-services/multiplayer/mpa/concepts/live-mpa-recent-players)中选择用户邀请加入游戏。当用户选择**确认**时，邀请将发送给选定的用户。

## 使用游戏自实现的自定义 UI

您的游戏可以实现自定义 UI，用于查看在线好友并邀请其加入游戏会话。
使用 [XblMultiplayerSendInvitesAsync](/reference/live/xsapi-c/multiplayer_c/functions/xblmultiplayersendinvitesasync) API 向由其 [XboxUserId (XUID)](/services/xbox-services/multiplayer/mpa/concepts/live-mpa-activities) 定义的一组用户发送邀请，如以下代码所示。
如果您希望使用自己的游戏内 UI 而不是标准系统 UI，这将很有用。

### Flat C

```cpp theme={null}
auto asyncBlock = std::make_unique<XAsyncBlock>();
asyncBlock->queue = queue;
asyncBlock->context = nullptr;
asyncBlock->callback = [](XAsyncBlock* asyncBlock)
{
    std::unique_ptr<XAsyncBlock> asyncBlockPtr{ asyncBlock }; // Take ownership of the XAsyncBlock*
    size_t handlesCount = 1; // must be equal to invites that are requested
    XblMultiplayerInviteHandle handles[1] = {};
    HRESULT hr = XblMultiplayerSendInvitesResult(asyncBlock, handlesCount, handles);
};

uint64_t xuids[1] = {};
xuids[0] = targetXuid;
size_t xuidsCount = 1;

HRESULT hr = XblMultiplayerSendInvitesAsync(
    xblContextHandle,
    &sessionReference,
    xuids,
    xuidsCount,
    titleId,
    contextStringId,
    customActivationContext,
    asyncBlock.get());
if (SUCCEEDED(hr))
{
    // The call succeeded, so release the std::unique_ptr ownership of XAsyncBlock* since the callback will take ownership.
    // If the call fails, the std::unique_ptr will keep ownership and delete the XAsyncBlock*
    asyncBlock.release();
}
```

## 参考 API 文档

* [Xgameactivation (API contents)](/reference/system/xgameactivation/xgameactivation_members)
* [xgameui (API contents)](/reference/system/xgameui/xgameui_members)
  * Functions
    * [XGameUiShowSendGameInviteAsync](/reference/system/xgameui/functions/xgameuishowsendgameinviteasync)
* [multiplayer\_c (API contents)](/reference/live/xsapi-c/multiplayer_c/multiplayer_c_members)
  * Functions
    * [XblMultiplayerSendInvitesAsync](/reference/live/xsapi-c/multiplayer_c/functions/xblmultiplayersendinvitesasync)
    * [XblMultiplayerSendInvitesResult](/reference/live/xsapi-c/multiplayer_c/functions/xblmultiplayersendinvitesresult)
  * Structures
    * [XblMultiplayerInviteHandle](/reference/live/xsapi-c/multiplayer_c/structs/xblmultiplayerinvitehandle)
* [xasync (API contents)](/reference/system/xasync/xasync_members)
  * Structures
    * [XAsyncBlock](/reference/system/xasync/structs/xasyncblock)

## 另请参见

[XAsyncBlock](/reference/system/xasync/structs/xasyncblock)
[XblMultiplayerInviteHandle](/reference/live/xsapi-c/multiplayer_c/structs/xblmultiplayerinvitehandle)
[XblMultiplayerSendInvitesAsync](/reference/live/xsapi-c/multiplayer_c/functions/xblmultiplayersendinvitesasync)
[XblMultiplayerSendInvitesResult](/reference/live/xsapi-c/multiplayer_c/functions/xblmultiplayersendinvitesresult)
[XGameActivation (API Reference)](/reference/system/xgameactivation/xgameactivation_members)
[使用 Multiplayer Manager 处理协议激活以启动游戏](/services/xbox-services/multiplayer/mpm/how-to/live-handle-protocol-activation)
[使用 Multiplayer Manager 发送游戏邀请](/services/xbox-services/multiplayer/mpm/how-to/live-send-game-invites)


## Related topics

- [向其他玩家发送邀请(流程图)](/zh-CN/services/xbox-services/multiplayer/mpm/concepts/flowcharts/live-mpm-send-invites.md)
- [发送邀请以邀请其他玩家加入多人游戏](/zh-CN/services/xbox-services/multiplayer/mpa/concepts/live-mpa-invites.md)
- [使用 Multiplayer Manager 发送游戏邀请](/zh-CN/services/xbox-services/multiplayer/mpm/how-to/live-send-game-invites.md)
- [邀请概述](/zh-CN/services/xbox-services/multiplayer/invites/live-multiplayer-invites-overview.md)
- [Multiplayer Activity 示例代码](/zh-CN/services/xbox-services/multiplayer/mpa/how-to/live-mpa-client-how-to.md)
