Click here to Skip to main content
15,867,308 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
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:
XML
<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:
C#
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();

            // 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;
        }
        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;
}
Posted
Comments
CHill60 8-Apr-16 5:35am    
If you put a break point on if (String.IsNullOrEmpty(displayName))
and examine the contents of displayName is there anything in the variable?
Luiey Ichigo 10-Apr-16 13:01pm    
I try to put a break point on string displayName = (string)data; but I don't know why the breakpoint was not being hit
CHill60 10-Apr-16 13:55pm    
That would imply either your page is never being loaded or an exception is being thrown before you get to that line. If you put a break on IReadOnlyList<user> users = await User.FindAllAsync(); do you get the breakpoint being hit and if so does the list of users get populated?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900