Introduction
In today's online world, making web applications personalized and engaging is important. A big part of this is letting users pick their profile pictures, which are the images that represent them on websites and in apps. Blazor, a web framework by Microsoft, makes it easy for developers to create interactive and dynamic web apps. In this article, we'll show you how to make a feature that lets users choose an avatar picture from a little popup window and set it as their profile picture in a Blazor app. We'll show you how to create this popup where users can view a variety of avatar pictures, pick one, and display it as their profile picture.
What is Blazor?
Blazor is a web framework created by Microsoft. It lets developers build interactive web apps using C# and .NET instead of the more commonly used JavaScript. With Blazor, you can do both the front-end and back-end of web development using a single programming language and system.
Prerequisites
Before we dive into the implementation, ensure you have the following prerequisites in place:
- Basic knowledge of C# and Blazor.
- A working Blazor application project.
- Some avatar images are ready to be used.
Step 1. Creating XML Data, Loading XML Data, Loading Images, and Defining the Avatar Model.
First, let's get comfortable with creating and using XML data and setting up the avatar model. You can learn all these essential steps by reading my article How to Display Avatar Images from XML Data in Blazor This article will give you the knowledge you need to work with XML data, load pictures, and define the avatar model in a simple and straightforward way.
Step 2. Define the Avatar Model.
Create an AvatarModel class to represent the avatar data. Create a class named AvatarModel.cs inside the Data folder, which is already present in the default project structure. This class will have properties for AvatarId, AvatarName, and AvatarUrl.
using System.Xml.Linq;
namespace DemoProject.Data
{
public class AvatarModel
{
public int AvatarId { get; set; }
public string AvatarName { get; set; }
public string AvatarUrl { get; set; }
}
}
Code Explanation
- AvatarModel is a C# class representing an avatar model.
- AvatarId is an integer property that likely represents a unique identifier for an avatar.
- AvatarName is a string property that likely represents the name or identifier of an avatar (e.g., "basketball-boy").
- AvatarUrl is a string property that likely represents the URL or path to the image file of the avatar (e.g., "images/Avatar/bird150x150.jpg").
Step 3. Display Avatars in the Popup and Choose One as Your Profile Picture.
Inside the ModelPopup.razor component, you can render the avatars from the loaded XML data in the popup modal, and you can also choose one as your profile picture.
@using DemoProject.Data
@inject AvatarService avatarService
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css">
<img src="@selectedAvatarUrl" alt="avatar" class="img-fluid avatar-image" />
@*<button class="btn-light" id="edit-profile" @onclick="Showpopup"></button>*@
<button class="btn-light" id="edit-profile" @onclick="Showpopup">
<i class="fas fa-pencil-alt"></i>
</button>
<div class="modal show taskmodalpopup" id="TaskModalPopUp" tabindex="-1" aria-hidden="true" style="display:@(IsShowEditPopup ? "block" : "none");">
<div class="modal-dialog" style="width:50%">
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title">Choose Your Avatar</h4>
<button type="button" class="btn-close" @onclick="CloseModal"></button>
</div>
<div class="modal-body">
<div class="row g-2 task-statusbar">
@foreach (var avatar in avatars)
{
<div class="col-lg-4 col-md-6 col-sm-12">
<div class="avatar-item">
<button class="btn-light" @onclick="() => SelectAvatar(avatar)">
<img src="@avatar.AvatarUrl" alt="@avatar.AvatarName" class="img-fluid" />
<h6>@avatar.AvatarName</h6>
<!-- Add a button to select this avatar -->
</button>
</div>
</div>
}
</div>
</div>
<div class="modal-footer">
<InputFile OnChange="HandleImageUpload" id="fileinput" class="custom-input-hide"></InputFile>
<button type="button" class="btn btn-danger" @onclick="CloseModal">Close</button>
</div>
</div>
</div>
</div>
@code {
public string selectedAvatarUrl { get; set; }
[Parameter]
public bool IsShowEditPopup { get; set; }
[Parameter]
public EventCallback OnClose { get; set; }
private void Showpopup()
{
IsShowEditPopup = true;
}
private void CloseModal()
{
IsShowEditPopup = false;
}
private List<AvatarModel> avatars;
protected override void OnInitialized()
{
// Specify the path to your XML file
string xmlFilePath = "app_data\\avatar.xml";
avatars = avatarService.LoadAvatarsFromFile(xmlFilePath);
}
private async Task HandleImageUpload(InputFileChangeEventArgs e)
{
var imageFile = e.File;
if (imageFile != null)
{
// Generate a unique file name or use the user's ID for naming
string uniqueFileName = Guid.NewGuid().ToString() + "_" + imageFile.Name;
// Get the path where you want to save the image (e.g., wwwroot/avatars/)
string imagePath = "wwwroot/avatars/" + uniqueFileName;
// Save the image to the specified path
using (var stream = new FileStream(imagePath, FileMode.Create))
{
await imageFile.OpenReadStream().CopyToAsync(stream);
}
selectedAvatarUrl = imageFile.Name;
}
// Update user's profile with the avatar information
}
private void SelectAvatar(AvatarModel avatar)
{
selectedAvatarUrl = avatar.AvatarUrl; // Set the selected avatar's URL as the profile picture
}
}
Join the conversation! Your thoughts help the community grow.