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 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could include an UITest?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have added the test case. Could you please review it and let me know if you have any concerns?

accept = ((IShellItemController)ShellItem).ProposeSection(r.ShellSection, false);
return accept;
Expand Down
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
37 changes: 37 additions & 0 deletions src/Controls/tests/TestCases.HostApp/Issues/Issue25599.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?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 Shell.TabBarForegroundColor="Green"
Shell.TabBarUnselectedColor="Red">
<Tab Title="Home" x:Name="FirstTab">
<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,27 @@
#if ANDROID // Facing a full exception when clicking on the already selected tab(App.Tap()) in Windows, iOS, and macOS, so excluded those platforms.
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");
App.Tap("Home");
App.WaitForElement("DetailsPageLabel");
App.WaitForNoElement("HomePageLabel"); //Navigation does not occur when clicking on an already selected tab
}
}
}
#endif