处理基于事件的统计信息更改

本主题提供处理基于事件的统计信息更改的示例代码。

若要获取有关基于事件的统计信息更改的通知

  1. 添加统计信息更改的处理程序
  2. 订阅特定的统计信息更改

若要清理:

  1. 删除统计信息更改的处理程序
  2. 取消订阅特定的统计信息更改

添加统计信息更改的处理程序

使用以下示例代码注册统计信息更改通知的事件处理程序。 事件处理程序接收 XblStatisticChangeEventArgs 对象。

平面 C API

void* context{ nullptr };
XblFunctionContext statisticChangedFunctionContext = XblUserStatisticsAddStatisticChangedHandler(
    xboxLiveContext,
    [](XblStatisticChangeEventArgs eventArgs, void* context) 
    {
        // Handle stat change. 
        LogToScreen("Statistic changed callback: stat changed (%s = %s)",
            eventArgs.latestStatistic.statisticName, 
            eventArgs.latestStatistic.value); 
    },
    context
);

有关详细信息,请参阅以下内容:

订阅特定的统计信息更改

使用以下示例代码,通过 XblUserStatisticsSubscribeToStatisticChange 处理程序订阅统计信息更新通知。

平面 C API

XblRealTimeActivitySubscriptionHandle subscriptionHandle{ nullptr };

HRESULT hr = XblUserStatisticsSubscribeToStatisticChange(
    xboxLiveContext,
    xboxUserId,
    scid,
    statisticName.c_str(),
    &subscriptionHandle
);

删除统计信息更改的处理程序

使用以下示例代码删除统计信息更改通知的事件处理程序。

平面 C API

XblUserStatisticsRemoveStatisticChangedHandler(
        xboxLiveContext,
        statisticChangedFunctionContext
);

取消订阅特定的统计信息更改

使用以下示例代码取消以前创建的统计信息更改订阅。

扁平 C API

hr = XblUserStatisticsUnsubscribeFromStatisticChange(
    xboxLiveContext,
    statisticChangeSubscriptionHandle
);