Recursive AI

Show avatar fallback initials in C#

OneApp is a free, open-source framework for apps in .NET whose UI is HTML and CSS, drawn natively with no web view on Windows, macOS, Linux, iOS and Android, and run in the browser, from Recursive AI. MIT or Apache-2.0, no native DLLs to ship.

This example opens a native window. For phone and tablet builds, see OneApp’s mobile setup.

Terminal
dotnet add package RecursiveAI.OneApp.Kit
dotnet add package RecursiveAI.OneApp
dotnet add package RecursiveAI.OneUI

Requires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.

HTML
<Avatar Src="missing-photo.png"
        Alt="Portrait of Maya Chen"
        Fallback="MC" />

The example is the full program below: unzip it and dotnet run.

This goes in ProfilePage.html. The sample deliberately leaves missing-photo.png unregistered. No image file or network connection is needed by the running app.

The full program

OneApp opens the window; OneApp.Kit supplies Avatar; OneUI compiles your markup.

Put ProfilePage.html next to ProfilePage.cs in the project folder. The OneUI markup compiler matches the template to the partial class by name, so no extra build item is needed.

ProfileDemo.csproj:

XML
<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net10.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference
      Include="RecursiveAI.OneApp"
      Version="1.1.1" />
    <PackageReference
      Include="RecursiveAI.OneApp.Kit"
      Version="1.1.1" />
    <PackageReference
      Include="RecursiveAI.OneUI"
      Version="1.1.0" />
  </ItemGroup>
</Project>

ProfilePage.html:

HTML
<Theme>
  <main style="padding: 24px">
    <Avatar Src="missing-photo.png"
            Alt="Portrait of Maya Chen"
            Fallback="MC" />
    <p>Maya Chen</p>
  </main>
</Theme>

ProfilePage.cs:

C#
public partial class ProfilePage
{
}

Program.cs opens the window with the ProfilePage component defined above:

C#
using OneApp;
 
var options = new AppOptions(new ProfilePage())
{
    Title = "Profile",
    Width = 340,
    Height = 190,
};
using var host = AppHost.Create(options);
host.Run();

The window shows MC above Maya Chen, with no terminal output (checked on Linux (X11)).

The Profile window showing MC inside a circle above the name Maya Chen

If you get a blank circle instead of initials when the picture is missing, check Fallback: Avatar doesn’t turn Alt or a display name into initials for you.

Variations

If you already know there’s no photo, omit Src. Use the small size for a compact contact list:

HTML
<Avatar Fallback="MC"
        Size="{OneApp.Kit.ControlSize.Sm}" />

The short answer

Set Fallback to the initials you want to display, Src to the photo’s address, and Alt to the photo’s text alternative. Leave Src out when there’s no photo. Keep the full name in the surrounding UI so users don’t have to guess who the initials represent.

How do I display initials when an avatar image fails to load?

Set Fallback even when you have a photo address. The kit renders the initials behind the image, so there’s already something to show if the image doesn’t draw. You don’t need a second component or an app-side error handler just to get initials. An omitted or empty Src makes the kit skip the image altogether.

In a native OneApp window, an image address isn’t a request to read that file from disk or fetch it from the web. The host resolves registered image addresses and supports data: images; see OneApp’s page image notes. An unregistered address is how this example demonstrates the fallback without shipping or downloading a photo. In an app that has contact data, use the initials supplied by the user or kept with the profile instead of assuming every name has two words.

What alt text should I use for an avatar?

Fallback is visible text; Alt is the text alternative for the image element. If a photo itself conveys meaningful information, describe that information briefly. If it just repeats the identity given by an adjacent name, Alt="" is appropriate so the name needn’t be read twice. The W3C’s alt text decision tree makes that distinction. Alt doesn’t label the entire avatar: when no image element is rendered, there’s no image alternative to read. Keep the full name independently readable in either case.

All oneapp recipes