> ## 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.

# 處理事件型統計資料變更

> 使用即時活動服務訂閱 XBOX Live 事件型統計資料變更通知並清除處理常式的 XSAPI 範例程式碼。

本主題提供處理事件型統計資料變更的範例程式碼。

**取得事件型統計資料變更的通知**

1. [新增統計資料變更處理常式](#ID4EM1)
2. [訂閱特定統計資料變更](#ID4EM2)

**清除**

1. [移除統計資料變更處理常式](#ID4EM3)
2. [取消訂閱特定統計資料變更](#ID4EM4)

<a id="ID4EM1" />

## 新增統計資料變更處理常式

使用下列範例程式碼，為統計資料變更通知註冊事件處理常式。
事件處理常式會收到 [XblStatisticChangeEventArgs](/zh-TW/reference/live/xsapi-c/user_statistics_c/structs/xblstatisticchangeeventargs) 物件。

#### Flat C API

```cpp theme={null}
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
);
```

如需詳細資訊，請參閱下列內容：

* [XblStatisticChangeEventArgs](/zh-TW/reference/live/xsapi-c/user_statistics_c/structs/xblstatisticchangeeventargs)
* [XblUserStatisticsAddStatisticChangedHandler](/zh-TW/reference/live/xsapi-c/user_statistics_c/functions/xbluserstatisticsaddstatisticchangedhandler)

<a id="ID4EM2" />

## 訂閱特定統計資料變更

使用下列範例程式碼，透過 `XblUserStatisticsSubscribeToStatisticChange` 處理常式訂閱統計資料更新通知。

#### Flat C API

```cpp theme={null}
XblRealTimeActivitySubscriptionHandle subscriptionHandle{ nullptr };

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

<a id="ID4EM3" />

## 移除統計資料變更處理常式

使用下列範例程式碼，移除統計資料變更通知的事件處理常式。

#### Flat C API

```cpp theme={null}
XblUserStatisticsRemoveStatisticChangedHandler(
        xboxLiveContext,
        statisticChangedFunctionContext
);
```

<a id="ID4EM4" />

## 取消訂閱特定統計資料變更

使用下列範例程式碼，取消先前建立的統計資料變更訂閱。

#### Flat C API

```cpp theme={null}
hr = XblUserStatisticsUnsubscribeFromStatisticChange(
    xboxLiveContext,
    statisticChangeSubscriptionHandle
);
```


## Related topics

- [事件型統計資料與排行榜範例程式碼](/zh-TW/services/xbox-services/player-data/stats-leaderboards/event-based/how-to/live-statslb-eb-howto-nav.md)
- [取得事件型統計資料](/zh-TW/services/xbox-services/player-data/stats-leaderboards/event-based/how-to/live-getting-eb-stat.md)
- [事件型統計資料與排行榜入口網站設定](/zh-TW/services/xbox-services/player-data/stats-leaderboards/event-based/config/live-statslb-eb-config-nav.md)
- [事件型統計資料概觀](/zh-TW/services/xbox-services/player-data/stats-leaderboards/event-based/live-stats-eb-overview.md)
- [事件型統計資料與排行榜](/zh-TW/services/xbox-services/player-data/stats-leaderboards/event-based/live-statslb-eb-nav.md)
