Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed Shell Navigating event issue when switching tabs #25749

Draft
wants to merge 9 commits into
base: main
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,9 @@ public override void ViewDidLoad()

ShouldSelectViewController = (tabController, viewController) =>
{
bool accept = true;
bool accept = false;
var r = RendererForViewController(viewController);
if (r != null)
if (r is not null && r != SelectedViewController)
accept = ((IShellItemController)ShellItem).ProposeSection(r.ShellSection, false);

return accept;
Expand Down
16 changes: 15 additions & 1 deletion src/Controls/src/Core/Handlers/Shell/ShellItemHandler.Windows.cs
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,22 @@ private void OnNavigationTabChanged(NavigationView sender, NavigationViewSelecti

var selectedItem = (NavigationViewItemViewModel)args.SelectedItem;

if (selectedItem.Data is ShellSection shellSection)
if (selectedItem.Data is ShellSection shellSection && VirtualView.Parent is Shell shell)
{
NavigationViewItemViewModel? currentItem = null;
foreach (var item in _mainLevelTabs)
{
if (shell.CurrentItem?.CurrentItem is not null && item.Data == shell.CurrentItem.CurrentItem)
{
currentItem = item;
break;
}
}
if (PlatformView is NavigationView navView && navView?.SelectedItem is not null && navView.SelectedItem != currentItem)
{
((IShellItemController)shell.CurrentItem!).ProposeSection(shellSection);
}

((Shell)VirtualView.Parent).CurrentItem = shellSection;
}
else if (selectedItem.Data is ShellContent shellContent)
Expand Down
36 changes: 36 additions & 0 deletions src/Controls/tests/TestCases.HostApp/Issues/Issue25599.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?xml version="1.0" encoding="utf-8" ?>
<Shell xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Maui.Controls.Sample.Issues.Issue25599"
xmlns:local="clr-namespace:Maui.Controls.Sample.Issues">
<TabBar>
<Tab Title="Home" x:Name="FirstTab" AutomationId="Tab1">
<ShellContent
Title="Home" >
<ShellContent.ContentTemplate>
<DataTemplate>
<ContentPage x:Name="firstPage">
<StackLayout>
<Button Text="Click" Clicked="Button_Clicked" AutomationId="HomePageButton"/>
<Label Text="Home Page" AutomationId="HomePageLabel"/>
</StackLayout>
</ContentPage>
</DataTemplate>
</ShellContent.ContentTemplate>
</ShellContent>
</Tab>
<Tab Title="Settings" x:Name="SecondTab">
<ShellContent>
<ShellContent.ContentTemplate>
<DataTemplate>
<ContentPage x:Name="secondPage">
<StackLayout>
<Label x:Name="settingsPageLabel" Text="SettingsPage" />
</StackLayout>
</ContentPage>
</DataTemplate>
</ShellContent.ContentTemplate>
</ShellContent>
</Tab>
</TabBar>
</Shell>
35 changes: 35 additions & 0 deletions src/Controls/tests/TestCases.HostApp/Issues/Issue25599.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
namespace Maui.Controls.Sample.Issues;

[Issue(IssueTracker.Github, 25599, "OnNavigating wrong target when tapping the same tab", PlatformAffected.iOS)]
public partial class Issue25599 : Shell
{
public Issue25599()
{
InitializeComponent();
}

private async void Button_Clicked(object sender, EventArgs e)
{
await Navigation.PushAsync(new DetailsPage());
}

public class DetailsPage : ContentPage
{
public DetailsPage()
{
Title = "DetailsPage";

Content = new StackLayout
{
Children =
{
new Label
{
Text = "Details Page",
AutomationId = "DetailsPageLabel"
}
}
};
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using NUnit.Framework;
using UITest.Appium;
using UITest.Core;

namespace Microsoft.Maui.TestCases.Tests.Issues
{
public class Issue25599 : _IssuesUITest
{
public Issue25599(TestDevice testDevice) : base(testDevice){ }

public override string Issue => "OnNavigating wrong target when tapping the same tab";

[Test]
[Category(UITestCategories.Navigation)]
public void NavigatingEventFired()
{
App.WaitForElement("HomePageButton");
App.Tap("HomePageButton");
App.WaitForElement("DetailsPageLabel");
#if ANDROID || WINDOWS
App.Tap("Home"); // Tapping already selected tab using Title
#elif MACCATALYST || IOS
App.Tap("Tab1"); // Tapping already selected tab using AutomationId
#endif
App.WaitForElement("DetailsPageLabel");
App.WaitForNoElement("HomePageLabel"); // Navigation does not occur when clicking on an already selected tab
}
}
}
Loading