Skip to content

Commit a217afc

Browse files
authored
Fix for Shell does not always raise Navigating event on Windows (#28315)
* Fix Changes and testcases * Update ShellItemHandler.Windows.cs * commit for label changes * Update ShellItemHandler.Windows.cs
1 parent dafe489 commit a217afc

File tree

3 files changed

+146
-2
lines changed

3 files changed

+146
-2
lines changed

src/Controls/src/Core/Handlers/Shell/ShellItemHandler.Windows.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,9 +169,15 @@ private void OnNavigationTabChanged(NavigationView sender, NavigationViewSelecti
169169

170170
((Shell)VirtualView.Parent).CurrentItem = shellSection;
171171
}
172-
else if (selectedItem.Data is ShellContent shellContent)
172+
else if (selectedItem.Data is ShellContent shellContent && VirtualView.Parent is Shell parentShell)
173173
{
174-
((Shell)VirtualView.Parent).CurrentItem = shellContent;
174+
// We need to invoke ProposeSection for TabBar items navigation for ShellContent
175+
var currentItem = parentShell.CurrentItem?.CurrentItem;
176+
if (currentItem?.Title != shellContent.Title && currentItem != shellContent.Parent)
177+
{
178+
(parentShell.CurrentItem as IShellItemController)?.ProposeSection(shellContent);
179+
}
180+
parentShell.CurrentItem = shellContent;
175181
}
176182
}
177183

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
using System.ComponentModel;
2+
using Maui.Controls.Sample.Issues;
3+
using Microsoft.Maui;
4+
using Microsoft.Maui.Controls;
5+
6+
namespace Controls.TestCases.Issues;
7+
8+
[Issue(IssueTracker.Github, 12500, "Shell does not always raise Navigating event on Windows", PlatformAffected.UWP)]
9+
public class Issue12500 : Shell
10+
{
11+
private NavigationViewModel _viewModel;
12+
public Issue12500()
13+
{
14+
this.FlyoutBehavior = FlyoutBehavior.Disabled;
15+
16+
_viewModel = new NavigationViewModel();
17+
18+
// Create TabBar
19+
var tabBar = new TabBar();
20+
21+
// Add ShellContent for MainPage
22+
tabBar.Items.Add(new ShellContent
23+
{
24+
Title = "Hello, World!",
25+
Route = "MainPage",
26+
ContentTemplate = new DataTemplate(() => new Issue12500Main { BindingContext = _viewModel })
27+
});
28+
// Add ShellContent for EventsPage
29+
tabBar.Items.Add(new ShellContent
30+
{
31+
Title = "Events",
32+
Route = "EventPage",
33+
ContentTemplate = new DataTemplate(() => new Issue12500EventPage { BindingContext = _viewModel })
34+
});
35+
36+
// Add TabBar to Shell
37+
this.Items.Add(tabBar);
38+
}
39+
protected override void OnNavigating(ShellNavigatingEventArgs args)
40+
{
41+
base.OnNavigating(args);
42+
string targetPageRoute = args.Target.Location.ToString();
43+
44+
// Update ViewModel with new navigation text
45+
_viewModel.LabelText = $"Navigating to {targetPageRoute}";
46+
47+
}
48+
}
49+
public class Issue12500EventPage : ContentPage
50+
{
51+
public Issue12500EventPage()
52+
{
53+
var label = new Label
54+
{
55+
AutomationId = "Issue12500EventPage",
56+
FontSize = 24,
57+
HorizontalOptions = LayoutOptions.Center,
58+
VerticalOptions = LayoutOptions.Center
59+
};
60+
label.SetBinding(Label.TextProperty, nameof(NavigationViewModel.LabelText)); // Bind to ViewModel
61+
62+
Content = new VerticalStackLayout
63+
{
64+
Children = { label }
65+
};
66+
}
67+
}
68+
69+
public class Issue12500Main : ContentPage
70+
{
71+
public Issue12500Main()
72+
{
73+
var label = new Label
74+
{
75+
AutomationId = "Issue12500MainPage",
76+
FontSize = 24,
77+
HorizontalOptions = LayoutOptions.Center,
78+
VerticalOptions = LayoutOptions.Center
79+
};
80+
label.SetBinding(Label.TextProperty, nameof(NavigationViewModel.LabelText)); // Bind to ViewModel
81+
82+
Content = new VerticalStackLayout
83+
{
84+
Children = { label }
85+
};
86+
}
87+
}
88+
89+
90+
public class NavigationViewModel : INotifyPropertyChanged
91+
{
92+
private string _labelText;
93+
94+
public string LabelText
95+
{
96+
get => _labelText;
97+
set
98+
{
99+
if (_labelText != value)
100+
{
101+
_labelText = value;
102+
OnPropertyChanged(nameof(LabelText));
103+
}
104+
}
105+
}
106+
107+
public event PropertyChangedEventHandler PropertyChanged;
108+
109+
protected virtual void OnPropertyChanged(string propertyName)
110+
{
111+
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
112+
}
113+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using NUnit.Framework;
2+
using UITest.Appium;
3+
using UITest.Core;
4+
5+
namespace Microsoft.Maui.TestCases.Tests.Issues;
6+
7+
public class Issue12500 : _IssuesUITest
8+
{
9+
public override string Issue => "Shell does not always raise Navigating event on Windows";
10+
11+
public Issue12500(TestDevice device) : base(device)
12+
{
13+
}
14+
15+
[Test]
16+
[Category(UITestCategories.Shell)]
17+
public void ShellNavigatingShouldTrigger()
18+
{
19+
App.WaitForElement("Issue12500MainPage");
20+
App.WaitForElement("Events");
21+
App.Tap("Events");
22+
var result = App.WaitForElement("Issue12500EventPage").GetText();
23+
Assert.That(result, Is.EqualTo("Navigating to //EventPage"));
24+
}
25+
}

0 commit comments

Comments
 (0)