【UWP开发】如何通过UWP获取系统用户Gamertag或者UserName等用户信息

13 篇文章 0 订阅

官方文档在此

如果使用xbox live sdk的话,直接通过下面的方式获取gamertag即可

async void LogIn() {
    Microsoft.Xbox.Services.System.XboxLiveUser m_user = new Microsoft.Xbox.Services.System.XboxLiveUser();
    SignInResult result = await m_user.SignInAsync(m_uiDispatcher);
    if (result.Status == SignInStatus.Success)
    {
        Debug.Log("UserName:"+m_user.Gamertag);
    }
}

但是如果当xboxlive因为某些情况(比如无网络)登录不了的时候,这时候XR又需要使用离线方式登录的时候,就需要自己通过接口获取用户名了

public async System.Threading.Tasks.Task<string> GetSystemUserName()
{
    var users = await Windows.System.User.FindAllAsync();

    if (users.Count > 0)
    {
        Windows.System.User activeUser = users[0];
        string displayName = (string)await activeUser.GetPropertyAsync(Windows.System.KnownUserProperties.DisplayName);

        Debug.Log("userName:" + displayName);

        return displayName;
    }
    return "";
}


但是使用上面的接口调用之后发现userId获取到了,而UserName获取到的是空字符串。
后来发现原因是项目没有配置好,按照如下配置即可获取到。


如果隐私设置里权限没有打开,获取的信息会返回空字符串。
如何判断隐私设置是否打开,可以调用如下接口:


if (Windows.System.UserProfile.UserInformation.NameAccessAllowed) {
   //隐私设置已打开
}

后来将游戏打包到Xbox One之后,发现无论怎么弄,获取到的DisplayName总是为空,而且Windows.System.KnownUserProperties里面的所有属性都尝试了,都是返回空,包括弹出的系统提示(是否允许应用访问用户名等信息)都是确定了,还是一样。

string displayName = (string)await activeUser.GetPropertyAsync(Windows.System.KnownUserProperties.DisplayName);
string AccountName = (string)await activeUser.GetPropertyAsync(Windows.System.KnownUserProperties.AccountName);
string FirstName = (string)await activeUser.GetPropertyAsync(Windows.System.KnownUserProperties.FirstName);
string LastName = (string)await activeUser.GetPropertyAsync(Windows.System.KnownUserProperties.LastName);

在后来谷歌了很长时间,看了N多帖子之后,看到了这篇帖子,然后按照如下方法尝试了,竟然真的获取到了!

public async System.Threading.Tasks.Task<string> GetSystemUserName()
{
    var users = await Windows.System.User.FindAllAsync();

    if (users.Count > 0)
    {
        Windows.System.User activeUser = users[0];
        string displayName = (string)await activeUser.GetPropertyAsync("Gamertag");

        Debug.Log("userName:" + displayName);

        return displayName;
    }
    return "";
}
也就是将GetPropertyAsync的参数改为字符串Gamertag,而不是用Windows.System.KnownUserProperties里面的常量,况且里面的常量也没有Gamertag!

string displayName = (string)await activeUser.GetPropertyAsync("Gamertag");
真是日了狗了!!!有问题你更新下官网的文档也好呀,UWP的坑真的太多了,真的是踏过无数的坑。

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

鱼蛋-Felix

如果对你有用,可以请我喝杯可乐

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值