Aller au contenu

Progression et notifications

Montrer qu'une tâche avance, et informer l'utilisateur sans le bloquer.

ProgressBar

Aperçu · déterminée / avec texte / indéterminée
65%
<ProgressBar Value="65" Maximum="100" />
<ProgressBar Value="65" ShowProgressText="True" />
<ProgressBar IsIndeterminate="True" />

Format du texte

<ProgressBar Value="3" Maximum="12" ShowProgressText="True"
             ProgressTextFormat="{}{0} / {3} fichiers" />

{0} valeur, {1} pourcentage, {2} minimum, {3} maximum.

Vertical, épais, coloré

Aperçu
<ProgressBar Orientation="Vertical" Height="120" Value="40" />
<ProgressBar Value="80" Height="12" MinHeight="12" CornerRadius="6" Foreground="#10B981" />

Mettre à jour depuis une tâche

sequenceDiagram
  participant UI as Thread UI
  participant T as Task.Run (arrière-plan)
  UI->>T: Traiter(progres)
  loop 100 fois
    T-->>UI: progres.Report(i)
    Note over UI: Barre.Value = i
  end
  T-->>UI: terminé (await rend la main)
private async void OnDemarrer(object? sender, RoutedEventArgs e)
{
    var progres = new Progress<double>(v => Barre.Value = v);   // revient sur le thread UI
    await Task.Run(() => Traiter(progres));
}

private static void Traiter(IProgress<double> progres)
{
    for (var i = 0; i <= 100; i++)
    {
        Thread.Sleep(30);
        progres.Report(i);
    }
}

Indicateur de chargement

Aperçu · voile semi-transparent par-dessus le contenu
Commandesn° 1001 — Adan° 1002 — Alann° 1003 — Grace
Chargement…
<Panel>
  <ContentControl Content="{Binding Contenu}" />
  <Border Background="#88FFFFFF" IsVisible="{Binding EstEnChargement}">
    <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center" Spacing="8">
      <ProgressBar IsIndeterminate="True" Width="160" />
      <TextBlock Text="Chargement…" HorizontalAlignment="Center" />
    </StackPanel>
  </Border>
</Panel>

Notifications

WindowNotificationManager affiche des toasts dans la fenêtre.

Aperçu · Position="BottomRight", les 4 types
Mon application
Information
Synchronisation en cours.
Warning
Espace disque faible.
Enregistré
Le document a été sauvegardé.
using Avalonia.Controls.Notifications;

public partial class MainWindow : Window
{
    private WindowNotificationManager? _notifications;

    protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e)
    {
        base.OnAttachedToVisualTree(e);
        _notifications = new WindowNotificationManager(TopLevel.GetTopLevel(this))
        {
            Position = NotificationPosition.BottomRight,
            MaxItems = 3
        };
    }

    private void OnEnregistrer(object? sender, RoutedEventArgs e)
        => _notifications?.Show(new Notification(
               "Enregistré",
               "Le document a été sauvegardé.",
               NotificationType.Success,
               TimeSpan.FromSeconds(3)));
}
NotificationType Usage
Information info neutre
Success action réussie
Warning attention
Error échec

Avec action au clic

_notifications?.Show(new Notification(
    "Mise à jour disponible", "Cliquez pour installer.",
    NotificationType.Information,
    expiration: TimeSpan.Zero,            // reste affichée
    onClick: () => Installer()));

Message dans la page (InfoBar maison)

Aperçu
Votre abonnement expire dans 3 jours.✕
<Border Background="#FEF3C7" BorderBrush="#F59E0B" BorderThickness="1,0,0,0"
        CornerRadius="4" Padding="12" IsVisible="{Binding AfficherAlerte}">
  <DockPanel>
    <Button DockPanel.Dock="Right" Content="✕" Padding="4" Background="Transparent"
            Command="{Binding FermerAlerteCommand}" />
    <TextBlock Text="Votre abonnement expire dans 3 jours." VerticalAlignment="Center" />
  </DockPanel>
</Border>