|
| 1 | +namespace Maui.Controls.Sample.Issues; |
| 2 | + |
| 3 | +[Issue(IssueTracker.Github, 31520, "NavigatingFrom is triggered first when using PushAsync", PlatformAffected.Android)] |
| 4 | +public class Issue31520 : NavigationPage |
| 5 | +{ |
| 6 | + public static int count = 0; |
| 7 | + public static Label statusLabel; |
| 8 | + public static Issue31520Page2 currentPage2; |
| 9 | + |
| 10 | + public Issue31520() : base(new Issue31520Page1()) |
| 11 | + { |
| 12 | + |
| 13 | + } |
| 14 | +} |
| 15 | + |
| 16 | +public class Issue31520Page1 : ContentPage |
| 17 | +{ |
| 18 | + public Issue31520Page1() |
| 19 | + { |
| 20 | + SetupPageContent(); |
| 21 | + |
| 22 | + Disappearing += (s, e) => |
| 23 | + { |
| 24 | + Issue31520.count++; |
| 25 | + }; |
| 26 | + |
| 27 | + NavigatingFrom += (s, e) => |
| 28 | + { |
| 29 | + if (Issue31520.statusLabel is not null) |
| 30 | + { |
| 31 | + Issue31520.statusLabel.Text = (Issue31520.count == 0) |
| 32 | + ? "NavigatingFrom triggered before Disappearing" |
| 33 | + : "NavigatingFrom triggered after Disappearing"; |
| 34 | + |
| 35 | + Issue31520.currentPage2?.UpdateNavigatingStatusLabel(); |
| 36 | + } |
| 37 | + }; |
| 38 | + } |
| 39 | + |
| 40 | + private void SetupPageContent() |
| 41 | + { |
| 42 | + Title = "Page 1"; |
| 43 | + |
| 44 | + Issue31520.statusLabel = new Label |
| 45 | + { |
| 46 | + Text = "Ready", |
| 47 | + AutomationId = "statusLabel" |
| 48 | + }; |
| 49 | + |
| 50 | + var button = new Button |
| 51 | + { |
| 52 | + Text = "Push", |
| 53 | + AutomationId = "PushButton" |
| 54 | + }; |
| 55 | + button.Clicked += OnCounterClicked; |
| 56 | + |
| 57 | + Content = new VerticalStackLayout |
| 58 | + { |
| 59 | + Padding = new Thickness(30, 0), |
| 60 | + Spacing = 25, |
| 61 | + Children = |
| 62 | + { |
| 63 | + Issue31520.statusLabel, |
| 64 | + button |
| 65 | + } |
| 66 | + }; |
| 67 | + } |
| 68 | + |
| 69 | + private void OnCounterClicked(object sender, EventArgs e) |
| 70 | + { |
| 71 | + Navigation.PushAsync(new Issue31520Page2()); |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +public class Issue31520Page2 : ContentPage |
| 76 | +{ |
| 77 | + private Label navigatingStatusLabel; |
| 78 | + |
| 79 | + public Issue31520Page2() |
| 80 | + { |
| 81 | + Issue31520.currentPage2 = this; |
| 82 | + |
| 83 | + Appearing += (s, e) => |
| 84 | + { |
| 85 | + Issue31520.count++; |
| 86 | + }; |
| 87 | + |
| 88 | + Title = "Page 2"; |
| 89 | + |
| 90 | + var button = new Button |
| 91 | + { |
| 92 | + Text = "Pop", |
| 93 | + AutomationId = "ChildPageButton" |
| 94 | + }; |
| 95 | + button.Clicked += OnButtonClicked; |
| 96 | + |
| 97 | + navigatingStatusLabel = new Label |
| 98 | + { |
| 99 | + Text = Issue31520.statusLabel?.Text ?? "Ready", |
| 100 | + AutomationId = "NavigatingStatusLabel" |
| 101 | + }; |
| 102 | + |
| 103 | + Content = new StackLayout |
| 104 | + { |
| 105 | + Children = |
| 106 | + { |
| 107 | + navigatingStatusLabel, |
| 108 | + button |
| 109 | + } |
| 110 | + }; |
| 111 | + } |
| 112 | + |
| 113 | + public void UpdateNavigatingStatusLabel() |
| 114 | + { |
| 115 | + if (navigatingStatusLabel is not null && Issue31520.statusLabel is not null) |
| 116 | + navigatingStatusLabel.Text = Issue31520.statusLabel.Text; |
| 117 | + } |
| 118 | + |
| 119 | + private void OnButtonClicked(object sender, EventArgs e) |
| 120 | + { |
| 121 | + Navigation.PopAsync(); |
| 122 | + } |
| 123 | +} |
0 commit comments