PWA Fundvelo der Caritas.
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

PageHistoryManager.cs 1.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. using Microsoft.AspNetCore.Components;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading.Tasks;
  5. namespace cwebplusApp.Shared.Services {
  6. public class PageHistoryManager {
  7. public EventCallback OnBeforeNavigateBack;
  8. private readonly List<string> previousPages;
  9. private NavigationManager NavigationManager;
  10. public PageHistoryManager(NavigationManager NavigationManager) {
  11. previousPages = new List<string>();
  12. this.NavigationManager = NavigationManager;
  13. }
  14. public void Reset() {
  15. previousPages.Clear();
  16. }
  17. public void AddPageToHistory(string pageName) {
  18. previousPages.Add(pageName);
  19. }
  20. public string GetPreviousPage() {
  21. if (CanGoBack()) {
  22. // You add a page on initialization, so you need to return the 2nd from the last
  23. string previousPage = previousPages.ElementAt(previousPages.Count - 2);
  24. previousPages.RemoveAt(previousPages.Count - 1);
  25. previousPages.RemoveAt(previousPages.Count - 1);
  26. return previousPage;
  27. }
  28. // Can't go back because you didn't navigate enough
  29. return previousPages.FirstOrDefault();
  30. }
  31. public bool CanGoBack() {
  32. return previousPages.Count > 1;
  33. }
  34. public async void NavigateBack() {
  35. await FireOnBeforeNavigateBackEvent();
  36. NavigationManager.NavigateTo(GetPreviousPage());
  37. }
  38. protected async Task FireOnBeforeNavigateBackEvent() {
  39. await OnBeforeNavigateBack.InvokeAsync();
  40. }
  41. }
  42. }