您的位置:首页 > 其它

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

2017-12-28 18:39 741 查看
官方文档在此

如果使用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的坑真的太多了,真的是踏过无数的坑。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐