用户名 System.User
Username with System.User
今天我想在我的应用程序中问候用户的名字,但我没有成功。
我找到了 System.User
,但缺少一些示例,我无法获得所需的信息。我认为不可能让当前用户 (id
) 调用 User.GetFromId()
.
你能指引我正确的方向吗?我走错路了吗?
好的,首先,访问用户的个人信息是您必须申请的特权,因此在您商店应用的 Package.appxmanifest 中,您需要启用 User Account Information
功能选项卡中的功能。
接下来,您需要使用 Windows.System.User class,而不是 System.User(System.User 不适用于 Windows 商店应用程序,根据您为问题提供的标签,您似乎正在讨论这些应用程序)
第三,您需要像这样索取个人信息。
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 时,您的用户会收到系统的权限提示,询问他们是否要授予您访问权限的权限。如果他们回答 'No',您将得到一个空的用户对象(但您仍然会得到一个唯一的令牌,如果他们再次使用该应用程序,您可以使用该令牌来区分该用户)。
如果他们回答是,您将能够访问以下属性以及其他各种属性。
请参阅 UserInfo Sample Microsoft 提供的更多示例。
今天我想在我的应用程序中问候用户的名字,但我没有成功。
我找到了 System.User
,但缺少一些示例,我无法获得所需的信息。我认为不可能让当前用户 (id
) 调用 User.GetFromId()
.
你能指引我正确的方向吗?我走错路了吗?
好的,首先,访问用户的个人信息是您必须申请的特权,因此在您商店应用的 Package.appxmanifest 中,您需要启用 User Account Information
功能选项卡中的功能。
接下来,您需要使用 Windows.System.User class,而不是 System.User(System.User 不适用于 Windows 商店应用程序,根据您为问题提供的标签,您似乎正在讨论这些应用程序)
第三,您需要像这样索取个人信息。
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 时,您的用户会收到系统的权限提示,询问他们是否要授予您访问权限的权限。如果他们回答 'No',您将得到一个空的用户对象(但您仍然会得到一个唯一的令牌,如果他们再次使用该应用程序,您可以使用该令牌来区分该用户)。
如果他们回答是,您将能够访问以下属性以及其他各种属性。
请参阅 UserInfo Sample Microsoft 提供的更多示例。