Hi,
I'm having problem to appearing the account user name. Can someone specify what's problem that I'm facing right now? Just first day of UWP and C#.
XAML:
<Page
x:Class="NISEApp.Logon"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:NISEApp"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" FontFamily="Segoe WP SemiLight" Loaded="Page_Loaded">
<Grid removed="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Hub x:Name="hub_nav"/>
<Rectangle Fill="#FF02AEF0" HorizontalAlignment="Left" Stroke="Black" Width="1024" StrokeThickness="0"/>
<Border Height="187" Margin="183,9,0,0" HorizontalAlignment="Left" Width="187" VerticalAlignment="Top">
<Border.Background>
<ImageBrush Stretch="Fill" ImageSource="Assets/admin.png"/>
</Border.Background>
</Border>
<TextBlock x:Name="tb_accname" HorizontalAlignment="Left" Height="51" Margin="8,211,0,0" TextWrapping="Wrap" Text="Administrator" VerticalAlignment="Top" Width="534" FontSize="36" TextAlignment="Center" Foreground="#FF636363" FontFamily="Segoe WP SemiLight"/>
<PasswordBox x:Name="txt_pwdbox" HorizontalAlignment="Left" Margin="110,282,0,0" VerticalAlignment="Top" Width="332" Height="50" BorderThickness="0" RequestedTheme="Light" FontSize="28"/>
<Button x:Name="button_verify" HorizontalAlignment="Left" Margin="451,273,0,0" VerticalAlignment="Top" Height="66" Width="66" RequestedTheme="Light" Click="verifypassword">
<Button.Background>
<ImageBrush Stretch="Fill" ImageSource="Assets/verify.png"/>
</Button.Background>
</Button>
<TextBlock x:Name="txt_error" HorizontalAlignment="Left" Height="51" Margin="10,376,0,0" TextWrapping="Wrap" Text="Invalid Password" VerticalAlignment="Top" Width="534" FontSize="36" FontFamily="Segoe WP SemiLight" TextAlignment="Center" Foreground="Red"/>
<CheckBox x:Name="cbx_show" Content="Show password" HorizontalAlignment="Left" Margin="110,339,0,0" VerticalAlignment="Top" RequestedTheme="Light" FontFamily="Segoe WP Light" FontSize="18" Checked="revealpasswordbox" Unchecked="revealpasswordbox"/>
<Button x:Name="button_back" HorizontalAlignment="Left" Margin="10,432,0,0" VerticalAlignment="Top" Height="66" Width="66" RequestedTheme="Light" Click="verifypassword">
<Button.Background>
<ImageBrush Stretch="Fill" ImageSource="Assets/back.png"/>
</Button.Background>
</Button>
</Grid>
</Page>
Code-Behind:
using System;
using System.Collections.Generic;
using System.Linq;
using Windows.System;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
namespace NISEApp
{
public sealed partial class Logon : Page
{
public Logon()
{
this.InitializeComponent();
txt_error.Visibility = Visibility.Collapsed;
}
private async void Page_Loaded(object sender, RoutedEventArgs e)
{
IReadOnlyList<User> users = await User.FindAllAsync();
var current = users.Where(p => p.AuthenticationStatus == UserAuthenticationStatus.LocallyAuthenticated &&
p.Type == UserType.LocalUser).FirstOrDefault();
var data = await current.GetPropertyAsync(KnownUserProperties.AccountName);
string displayName = (string)data;
if (String.IsNullOrEmpty(displayName))
{
string a = (string)await current.GetPropertyAsync(KnownUserProperties.FirstName);
string b = (string)await current.GetPropertyAsync(KnownUserProperties.LastName);
displayName = string.Format("{0} {1}", a, b);
}
tb_accname.Text = displayName;
}
private void revealpasswordbox(object sender, RoutedEventArgs e)
{
if (cbx_show.IsChecked == true)
{
txt_pwdbox.PasswordRevealMode = PasswordRevealMode.Visible;
}
else
{
txt_pwdbox.PasswordRevealMode = PasswordRevealMode.Hidden;
}
}
private void verifypassword(object sender, RoutedEventArgs e)
{
if (txt_pwdbox.Password != null)
{
}else
{
txt_error.Text = "Password field cannot be null";
txt_error.Visibility = Visibility.Visible;
}
}
}
}
What I have tried:
private async void Page_Loaded(object sender, RoutedEventArgs e)
{
IReadOnlyList<user> users = await User.FindAllAsync();
var current = users.Where(p => p.AuthenticationStatus == UserAuthenticationStatus.LocallyAuthenticated &&
p.Type == UserType.LocalUser).FirstOrDefault();
// user may have username
var data = await current.GetPropertyAsync(KnownUserProperties.AccountName);
string displayName = (string)data;
//or may be authinticated using hotmail
if (String.IsNullOrEmpty(displayName))
{
string a = (string)await current.GetPropertyAsync(KnownUserProperties.FirstName);
string b = (string)await current.GetPropertyAsync(KnownUserProperties.LastName);
displayName = string.Format("{0} {1}", a, b);
}
tb_accname.Text = displayName;
}