User Info In Universal Windows Programs

Windows 10 Settings

In Windows 10 the user must explicitly give permission to access the user information data. The first time it runs the application, Windows 10 privacy settings automatically prompt the message box, to confirm whether the application can read the user information or not.

user

Later at some point of time, ifthe  user doesn't like the application accessing information, then he or she can disable it (go to settings -> Privacy -> Account -> select the application and disable,

application

Application Setting

Application must enable the User Account information in the Package.appxmanifest file, otherwise the application can’t access the user information.

Let us see a simple example of how to read user name and display account information. 

Define the User Modal class,
  1. public class UniversalUserInfo: INotifyPropertyChanged  
  2. {  
  3.     private string _userName;  
  4.     public string UserName  
  5.     {  
  6.         get  
  7.         {  
  8.             return _userName;  
  9.         }  
  10.         set  
  11.         {  
  12.             _userName = value;  
  13.             OnPropertyChanged(nameof(UserName));  
  14.         }  
  15.     }  
  16.   
  17.     private BitmapImage _imgStream;  
  18.     publi BitmapImageImgStream   
  19.     {  
  20.         get  
  21.         {  
  22.             return _imgStream;  
  23.         }  
  24.         set  
  25.         {  
  26.             _imgStream = value;  
  27.             OnPropertyChanged(nameof(ImgStream));  
  28.         }  
  29.     }  
  30.     private string _userType;  
  31.   
  32.     public string UserType   
  33.     {  
  34.         get  
  35.         {  
  36.             return _userType;  
  37.         }  
  38.         set  
  39.         {  
  40.             _userType = value;  
  41.             OnPropertyChanged(nameof(UserType));  
  42.         }  
  43.     }  
  44.   
  45.     private string _authenticationStatus;  
  46.   
  47.     public string AuthenticationStatus  
  48.     {  
  49.         get  
  50.         {  
  51.             return _authenticationStatus;  
  52.         }  
  53.         set   
  54.         {  
  55.             _authenticationStatus = value;  
  56.             OnPropertyChanged(nameof(AuthenticationStatus));  
  57.         }  
  58.     }  
  59.   
  60.   
  61.     public eventPropertyChangedEventHandlerPropertyChanged;  
  62.     protected virtual voidOnPropertyChanged(string propertyName = null)  
  63.     {  
  64.         PropertyChanged ? .Invoke(this, new PropertyChangedEventArgs(propertyName));  
  65.     }  
  66. }  
User class using to read the user information

User.FindAllAsync(); this function used to get all the user information and read each user's properties, such as name or picture name.  Use the KnowUserProperties to get all user properties.

There are wo ways we can pass this KnowUserProperties -- single parameter passes or multiple properties

User.GetPropertyAsync() – returns single properties value

Ex: User.GetPropertyAsync(KnownUserProperties.AccountName);

User.GetPropertiesAsync passes the collection of properties and returns the collection of properties value

Ex:
  1. User.GetPropertiesAsync(newList < string > ()  
  2.  {  
  3.     KnownUserProperties.DisplayName,  
  4.         KnownUserProperties.DisplayName  
  5. });  
This article example uses the collection of the properties to get all the user information. The following function is used to prepare the collection of the Properties,

Properties

Read the picture information, call the GetPictureAsync function, and pass the size (Note: read the picture information it’s not under the KnownUserProperties).

User.GetPictureAsync(UserPictureSize.Size424x424);

And function return the IRandomAccessStreamReference to display to the GUI, and convert to the Bitmap image,

function

User AuthenticationStatus and UserType information are Enum types, the below function uses to convert the Enum to String,

String

Find Users () function to read the complete information and assign the UniversalUserInfo class,

class


Here's the output:

output

Find the complete source code


Similar Articles