Getting started
In this tutorial you build a small orders dashboard. You start with the headless controller in a console app to see how resources and filters behave, then render the same idea in a Blazor page.
On this page
Check the prerequisites first: the .NET 11 SDK, and for the components a Blazor app with Klyroo.UI set up.
1. Install the packages
For the headless part, in any .NET 11 project:
dotnet add package Klyroo.Dashboard.Core --prereleaseFor the Blazor part, in your Blazor Web App:
dotnet add package Klyroo.Dashboard --prereleaseKlyroo. brings Klyroo. and Klyroo. with it.
2. Build a headless dashboard
A Dashboard owns the filters, the views and the resources of one dashboard. A Dashboard wraps one query. Put this in a console app:
using Klyroo.Dashboard;
var controller = new DashboardController("orders");
var status = controller.AddFilter(new DashboardFilterControl(
"status", "Status",
items: [new("open", "Open"), new("shipped", "Shipped")],
defaultValues: ["open"]));
var orders = controller.RegisterResource(new DashboardResource<int>(
"orders", _ => CountOrdersAsync(status.Value)));
await controller.RefreshAsync();
Console.WriteLine($"{orders.State}: {orders.Data}"); // Ready: 12
status.Toggle("shipped"); // the selection changed...
await controller.RefreshAsync(); // ...but resources only reload when you ask
Console.WriteLine($"{orders.State}: {orders.Data}"); // Ready: 30
static Task<int> CountOrdersAsync(string? status) =>
Task.FromResult(status == "open" ? 12 : 30);Two things to notice. The loader reads the filter through a closure, so the controller never needs to know what a filter means for a query. And changing a filter does not reload anything by itself: the next step shows the Blazor equivalent, and the guides show how to reload automatically.
3. Register the services
In Program. of the Blazor Web App, and in the . project when you use WebAssembly or Auto:
using Klyroo.Dashboard;
builder.Services.AddKlyrooDashboard();In a .NET MAUI Blazor Hybrid app, call it in MauiProgram.. AddKlyroo calls AddKlyrooUi(), so you do not register Klyroo.UI separately.
4. Add the imports
In _:
@using Klyroo.UI
@using Klyroo.Dashboard5. Load the stylesheets
In the <head> of App., load the Klyroo.UI styles and then the dashboard stylesheet. The dashboard stylesheet contains the utilities used by the dashboard markup and relies on the tokens defined by Klyroo.UI, so the order matters:
<link rel="stylesheet" href="@Assets["_content/Klyroo.UI/klyroo.preflight.css"]" />
<link rel="stylesheet" href="@Assets["_content/Klyroo.UI/klyroo.css"]" />
<link rel="stylesheet" href="@Assets["_content/Klyroo.Dashboard/klyroo-dashboard.css"]" />In a MAUI Blazor Hybrid app, use the same _ paths in wwwroot/ without @Assets[...].
6. Render the dashboard
Create Components/. SalesService, Summary and RegionSales stand for your own data access and records:
@page "/orders"
@rendermode InteractiveServer
@inject SalesService Sales
<KlyDashboardPage Dashboard="_dashboard" Title="Orders">
<KlyDashboardGrid>
<KlyDashboardStat Source="_summary" Label="Orders" Span="4"
Value="_summary.Data?.Orders" Format="DashboardFormatPreset.Integer" />
<KlyDashboardBars Source="_regions" Title="Revenue by region" Span="8"
Items="_regions.Data?.Select(r => new DashboardBarItem(r.Region, r.Revenue)).ToList()"
Format="DashboardFormatPreset.Compact" />
</KlyDashboardGrid>
</KlyDashboardPage>
@code {
private readonly DashboardController _dashboard = new("orders");
private DashboardResource<Summary> _summary = default!;
private DashboardResource<IReadOnlyList<RegionSales>> _regions = default!;
protected override async Task OnInitializedAsync()
{
_summary = _dashboard.RegisterResource(
new DashboardResource<Summary>("summary", ct => Sales.GetSummaryAsync(null, ct)));
_regions = _dashboard.RegisterResource(
new DashboardResource<IReadOnlyList<RegionSales>>(
"regions", ct => Sales.GetRegionsAsync(null, ct), DashboardQueryStage.Background));
await _dashboard.RefreshAsync();
}
}The @rendermode line assumes the @using static Microsoft. that the Blazor Web App template puts in _. Run the app and open /. The two blocks first show skeletons. The stat appears when the essential query finishes, then the bars appear when the background query finishes. If a query throws, only its block turns into an error message with a Retry button.
Three details of the page are worth knowing:
- The
Sourceparameter only drives the state of the block (skeleton, error, empty, content). The block never readsDatafrom the source, so you pass_yourself.summary. Data?. Orders KlyDashboardPagesubscribes to the controller and re-renders when any resource changes. A block rendered outside a page does not update on its own.- The page draws the view tabs and the filter pills, but you render the content of each view yourself (see Load a block only when its tab opens).