如何在Windowsapp store应用中登录PC用户名?
像在这个线程中如何使用C#获取.NET中的当前用户名?
但是代码
string userName = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
似乎不适用于Windows 8应用程序。
我怎样才能做到这一点?
步骤1:
使用C#和XAML语言创建空白Windowsapp store应用程序。
第2步:
x:Class="SetAccountPicture.UserProfileInformation" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:SetAccountPicture" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d">
第3步:
using Windows.Storage; using Windows.Storage.Pickers; using Windows.Storage.Streams; using Windows.System.UserProfile; using Windows.UI.Core; using Windows.UI.Xaml.Media.Imaging;
第4步:
string displayName = await UserInformation.GetDisplayNameAsync(); if (string.IsNullOrEmpty(displayName)) { rootPage.NotifyUser("No Display Name was returned", NotifyType.StatusMessage); } else { UserName.Text = displayName; }
获取当前用户的第一个名称。
注意:这仅适用于Microsoft帐户。
string firstName = await UserInformation.GetFirstNameAsync(); if (string.IsNullOrEmpty(firstName)) { rootPage.NotifyUser("No Display Name was returned", NotifyType.StatusMessage); } else { FistName.Text = firstName; }
获取当前用户的姓氏; 看到:
string lastName = await UserInformation.GetLastNameAsync(); if (string.IsNullOrEmpty(lastName)) { rootPage.NotifyUser("No Display Name was returned", NotifyType.StatusMessage); } else { LastName.Text = lastName; }
获取当前用户的帐户图片。
注意:您可以请求三种类型的图像。 例如:小,大和video(动态图像)。 如果可用,则返回。
StorageFile image = UserInformation.GetAccountPicture(AccountPictureKind.SmallImage) as StorageFile; if (image != null) { rootPage.NotifyUser("SmallImage path = " + image.Path, NotifyType.StatusMessage); try { IRandomAccessStream imageStream = await image.OpenReadAsync(); BitmapImage bitmapImage = new BitmapImage(); bitmapImage.SetSource(imageStream); AccountPicture.Source = bitmapImage; } catch (Exception ex) { rootPage.NotifyUser("Error opening stream: " + ex.ToString(), NotifyType.ErrorMessage); } } else { rootPage.NotifyUser("Small Account Picture is not available", NotifyType.StatusMessage); }
图片和源代码: