Lifecycle & orchestration
A running tour is a session you can drive: move it, pause it, resume it, or end it in one of several ways that each mean something different. Tours that want the screen at the same time wait their turn, a user can ask to be reminded later, every step has hooks for your own code, and a definition is checked before anyone sees it.
Drive a tour from code
StartAsync returns the running IBonboardingTourSession. Its commands move the tour, its
properties say where it stands, and its Changed event fires on every change - the panel under the
product re-renders from it. This tour sets Interaction = Free, so the page stays usable while the
card is up and the buttons below can drive it, and ShowOverlay = false, so the target keeps its
spotlight ring without the page being dimmed.
Start the session to see its state here.
The ways a tour can end are not interchangeable - each is remembered differently:
CompleteAsync- finished: recorded as completed, and never offered again byStartOnceAsync.SkipAsyncandDismissAsync- the user turned it down, with the Skip button, the close button or Escape. It is not offered again either.CancelAsync- interrupted, not refused: the progress is kept,StartOnceAsyncoffers the tour again andStartAsyncresumes it. Starting another tour cancels the running one.SnoozeAsync(duration)- "remind me later": see below.PauseAsyncandResumeAsync- hide the card and keep the session exactly where it is; nothing ends.
The events page shows the record each ending leaves in the store.
var session = await BonboardingService.StartAsync(tour, new BonboardingStartOptions
{
StartAtStepId = "metrics", // begin here instead of at the first step
Resume = false, // ignore saved progress
State = new Dictionary<string, object?> { ["plan"] = "pro" }, // seeds session.State
});
// Changed fires on every change; e.Change says what: Step, Geometry and/or Ended.
session.Changed += (_, e) => InvokeAsync(StateHasChanged);
await session.NextAsync();
await session.PreviousAsync(); // follows the visit history, not the declaration order
await session.GoToAsync("settings"); // by id - or GoToAsync(3), by index
await session.PauseAsync(); // hide it, keep its place
await session.ResumeAsync();
await session.CancelAsync(); // interrupted: progress kept, offered again
await session.CompleteAsync(); // finished, recorded as completed
await session.RefreshAsync(); // re-measure after a layout change of your own
session.Status // Idle, Running, Waiting, Paused, Completed, Skipped, Dismissed, Cancelled, Failed, Snoozed
session.CurrentStep // the BonboardingStepDefinition on screen
session.DisplayPosition // 1-based, counting only the steps that will be shown, of DisplayCount
session.History // the ids of the steps actually visited
session.EndReason // how it ended, once it has
await BonboardingService.StopAllAsync(); // cancel every running and queued tourWhen two tours want the screen
Start the first tour, then use the buttons it points at to start another while it runs.
BonboardingStartOptions.WhenBusy decides what happens: Replace - the default, right for a
start the user asked for - cancels the running tour; Queue waits for it to end; Skip does
not start at all. Queued tours start one at a time, the highest Priority first, and are checked again
when their turn comes. Queue both, then finish the first tour and watch the order in the log.
A declarative tour's AutoStart always queues, so a page's own tour never takes the screen from a tour
the user is in the middle of.
// A start the user asked for: replaces whatever runs (WhenBusy = Replace, the default).
await BonboardingService.StartAsync(helpTour);
// A start nobody asked for: wait for the running tour to end - or Skip, and do not start at all.
await BonboardingService.StartOnceAsync(featureTour, new BonboardingStartOptions
{
WhenBusy = BonboardingBusyBehavior.Queue,
});
// Among queued tours, the highest Priority goes first; equal priorities in the order they were queued.
new BonboardingTourDefinition { Id = "billing-changes", Priority = 5, ... }
<BonboardingTour Id="welcome" AutoStart="BonboardingAutoStart.Once" Priority="1"> ... </BonboardingTour>
// To run several at once - a tour and a persistent hint - opt out of all of it:
await BonboardingService.StartAsync(hint, new BonboardingStartOptions { StopOthers = false });"Remind me later"
BonboardingStepAction.RemindMeLater adds a button that snoozes the tour: the record keeps the user's
place and gets a SnoozedUntil. Until then StartOnceAsync and AutoStart pass
the tour over; afterwards it resumes where it was snoozed. A start the user asks for (StartAsync) is
never held back. Offer the announcement, press Remind me in 30 seconds, and offer it again.
One rule covers every automatic start at once: BonboardingOptions.CanAutoStart. This demo registers one
that reads a "turn tips off" preference. Switch it on and offering the announcement (or any page's
AutoStart tour) does nothing, while Show it anyway - a start the user asked for - still
shows it.
new BonboardingStepDefinition
{
Title = "Three new things",
DoneLabel = "Got it",
Actions = [BonboardingStepAction.RemindMeLater("Remind me in 30 seconds", TimeSpan.FromSeconds(30))],
}
var session = await BonboardingService.StartOnceAsync(tour); // null while snoozed, completed or dismissed
var record = await BonboardingService.GetRecordAsync(tour.Id); // record.SnoozedUntil, record.IsSnoozedAt(now)
await session.SnoozeAsync(TimeSpan.FromDays(3)); // the same, from codeHooks for your own code
Each step can run code before it is shown (OnBeforeShow, awaited - the place to load what it needs),
once it is on screen (OnShown) and before it is left (OnBeforeLeave); the tour has
OnStarted, OnStepChanged and OnEnded. Every hook is handed the session's
State, a dictionary shared by the whole tour, the application's services, and a cancellation token
that fires when the step is left. Throwing from a hook fails the tour cleanly; it never escapes as an unobserved
exception.
Here the first step waits a second in OnBeforeShow before its card appears. The second step's answer
is written to State by its action, and the third step's When reads it: conditions are
asked again on every transition, so an earlier step can unlock a later one - watch the progress count grow when
you press Connect.
new BonboardingTourDefinition
{
OnStarted = ctx => ...,
OnStepChanged = args => ..., // args.Step, args.PreviousStep, args.Reason (Start, Next, Previous, Jump, Resume, Retarget)
OnEnded = args => ..., // args.Reason, args.IsSuccess, args.LastStep, args.Error
Steps =
{
new BonboardingStepDefinition
{
Id = "load",
OnBeforeShow = async ctx => ctx.State["data"] = await LoadAsync(ctx.CancellationToken),
OnShown = ctx => ...,
OnBeforeLeave = ctx => ..., // throwing here fails the tour; use CanAdvance to hold the user
},
new BonboardingStepDefinition
{
Id = "connect",
ShowNext = false,
Actions =
[
new BonboardingStepAction
{
Label = "Connect",
Style = BonboardingStepActionStyle.Primary,
OnClick = ctx => { ctx.State["connected"] = true; return ValueTask.CompletedTask; },
Effect = BonboardingStepActionEffect.Next,
},
new BonboardingStepAction { Label = "Not now", Style = BonboardingStepActionStyle.Ghost, Effect = BonboardingStepActionEffect.Next },
],
},
new BonboardingStepDefinition
{
Id = "deploy",
When = ctx => ValueTask.FromResult(ctx.State.ContainsKey("connected")),
},
},
}Checking a definition
StartAsync checks a tour before anything is shown. Errors - two steps sharing an id,
an action that jumps to a step that does not exist - refuse the start with a BonboardingException
listing every one of them, because the tour could not work. Warnings - a step with no way forward,
an opacity outside 0 to 1 - are logged and the tour starts. Validate() returns the same list, so a
unit test catches a mistake before a user meets it.
foreach (var issue in tour.Validate())
Console.WriteLine(issue); // Error duplicate-step-id: Tour 'broken' has more than one step with the id 'intro'. ...
// In a unit test, so a mistake never reaches a user:
[TestMethod]
public void The_welcome_tour_is_well_formed()
=> Assert.IsEmpty(WelcomeTour.Create().Validate());