User.GetUserAgeRangeAsync 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
检索当前用户所属于的年龄范围。
public:
virtual IAsyncOperation<UserAgeRange ^> ^ GetUserAgeRangeAsync() = GetUserAgeRangeAsync;
/// [Windows.Foundation.Metadata.RemoteAsync]
IAsyncOperation<UserAgeRange> GetUserAgeRangeAsync();
[Windows.Foundation.Metadata.RemoteAsync]
public IAsyncOperation<UserAgeRange> GetUserAgeRangeAsync();
function getUserAgeRangeAsync()
Public Function GetUserAgeRangeAsync () As IAsyncOperation(Of UserAgeRange)
返回
为当前用户返回 UserAgeRange 的异步操作,或者 null 当年龄范围未知或不可用时。
- 属性
Windows 要求
| 要求 | 说明 |
|---|---|
| 设备系列 |
Windows 11, version 24H2 (在 10.0.26100.0 中引入)
|
| API contract |
Windows.Foundation.UniversalApiContract (在 v19.0 中引入)
|
| 应用功能 |
userAccountInformation
|
示例
#include <winrt/Windows.Foundation.h>
#include <winrt/Windows.System.h>
using namespace winrt;
using namespace winrt::Windows::System;
int wmain()
{
init_apartment();
User user = User::GetDefault();
UserAgeRange range = user.GetUserAgeRangeAsync().get();
if (range)
{
int32_t lower = range.Lower();
int32_t upper = range.Upper();
// Use the inclusive age bucket.
}
else
{
// Use the app's fallback experience.
}
}
User user = User.GetDefault();
UserAgeRange range = await user.GetUserAgeRangeAsync();
if (range != null)
{
int lower = range.Lower;
int upper = range.Upper;
// Use the inclusive age bucket.
}
else
{
// Use the app's fallback experience.
}
注解
UserAgeRange.Lower 并且 UserAgeRange.Upper 是非独占边界。
| 年龄组 | Lower |
Upper |
|---|---|---|
| 低于 10 | 0 | 9 |
| 10-12 | 10 | 12 |
| 13-15 | 13 | 15 |
| 16-17 | 16 | 17 |
| 18 岁或以上 | 18 | INT32_MAX |
操作在 null 用户年龄未知、功能不可用或管理策略禁用 API 时返回。
null结果是预期的结果,不能被视为确切的年龄或 18 或更早的范围。
- 对运行当前进程的用户的 User 对象调用此方法。 使用 User.GetDefault 直接获取该用户。
- 应用包必须声明
userAccountInformation该功能。 没有访问权限的调用方或对其他用户使用User对象的调用方可以接收E_ACCESSDENIED。 - 此方法返回年龄桶,而不是用户的确切年龄或出生日期。
- 在 C++/WinRT 中,从合适的非 UI 线程调用
.get()或使用co_await协同例程。
管理策略
管理员可以禁用数字安全年龄 API,或者通过组策略或 MDM 配置默认年龄组。 禁用 API 时,此方法返回 null。 配置默认年龄组时,此方法返回 UserAgeRange 与该组对应的组。