用户名与 System.User [英] Username with System.User

查看:27
本文介绍了用户名与 System.User的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

今天我想通过名字向我的应用程序中的用户打招呼,但我没有设法得到它.

Today I wanted to greet the user in my app by name, but I did not manage to get it.

我找到了 System.User,但由于缺少一些示例,我无法获得所需的信息.我认为不可能让当前用户 (id) 调用 User.GetFromId().

I found System.User, but lacking some examples I did not manage to get the info I needed. I saw no possibility to get the current user (id) to call User.GetFromId().

你能指导我走向正确的方向吗?我是不是走错路了?

Can you guide me into the right direction? Have I been on the wrong path?

推荐答案

好的,首先,访问用户的个人信息是您必须请求的特权,因此在您的商店应用程序的 Package.appxmanifest 中,您需要在 Capabilities 选项卡中启用 User Account Information 功能.

Okay, So first things first, getting access to a user's personal information is a privilege you have to request, so in your store app's Package.appxmanifest, you'll need to enable the User Account Information capability in the Capabilities tab.

接下来,您将希望使用 Windows.System.User 类,而不是 System.User(System.User 不适用于 Windows 商店应用程序,鉴于您提供的标签,您似乎正在讨论该类你的问题)

Next, you'll want to be using the Windows.System.User class, not the System.User (System.User isn't available to Windows store apps, which you appear to be discussing given the tags you provided for your question)

第三,您需要像这样请求个人信息.

Third, you'll want to request personal information like this.

IReadOnlyList<User> users = await User.FindAllAsync(UserType.LocalUser, UserAuthenticationStatus.LocallyAuthenticated);
User user = users.FirstOrDefault();
if (user != null)
{
   String[] desiredProperties = new String[]
   {
      KnownUserProperties.FirstName,
      KnownUserProperties.LastName,
      KnownUserProperties.ProviderName,
      KnownUserProperties.AccountName,
      KnownUserProperties.GuestHost,
      KnownUserProperties.PrincipalName,
      KnownUserProperties.DomainName,
      KnownUserProperties.SessionInitiationProtocolUri,
   };

   IPropertySet values = await user.GetPropertiesAsync(desiredProperties);
   foreach (String property in desiredProperties)
   {
      string result;
      result = property + ": " + values[property] + "\n";
      System.Diagnostics.Debug.WriteLine(result);
   }
}

当您调用 GetPropertiesAsync 时,您的用户将收到来自系统的权限提示,询问他们是否要授予您访问权限.如果他们回答否",您将获得一个空的用户对象(但您仍将获得一个唯一令牌,如果他们再次使用该应用程序,您可以使用该令牌来区分该用户).

When you call GetPropertiesAsync, your user will get a permission prompt from the system asking them if they want to give you access to that. If they answer 'No', you'll get an empty user object (but you'll still get a unique token you can use to distinguish that user if they use the app again).

如果他们的回答是肯定的,您将能够访问以下属性以及其他各种属性.

If they answer yes, you'll be able to get access to the properties below, and various others.

请参阅 Microsoft 提供的 UserInfo 示例以了解更多信息示例.

See the UserInfo Sample Microsoft provided for more examples.

这篇关于用户名与 System.User的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆