Sunny Duckling
Sunny Duckling

Reputation: 357

How can I add user's name in WPF?

I'm creating a UI App on C# using .NET Core .

I have to register users and let them login. So, the question is: how may I welcome logged user? I want the following: "Welcome back, {UserName}".

I create new window with input of class User object, so I know where to get his/her name, but I'm not sure how to program adding the name in .xaml.

Would be grateful for any possible help!

Upvotes: 0

Views: 691

Answers (2)

Yohann
Yohann

Reputation: 234

From your description, you may created a wpf Application(net core 3.0)?

When you logged from the log form. You can show the "Welcome back, {UserName}" in the main form. Like the following simple code.

We can define a User object property in App class.

public partial class App : Application
{
    public static Users usersd { get; set; }

}

And assign the User object when logging. Then, in the Main Windows/others form, you can show the user information:

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        if (App.usersd != null)
        {
            label.Content = "Welcome back, {" + App.usersd.Name + "}";
        }
    }

enter image description here

Upvotes: 1

Mayfair
Mayfair

Reputation: 647

In order to welcome users back, you need to store the user data in a database or in a file(.txt,.json,..). One way to do this is storing the user data as JSON when they first open the program. link

Then, you can check if the user has already logged in before. (perhaps use a userID property in User class)

Don't forget you need to add a NuGet package called Newtonsoft.Json in order to serialize and deserialize the data. Install NuGet package

Upvotes: 0

Related Questions