Skip to content

Commit

Permalink
Fix ShellContent Title doesn't observe changes to bound properties (#…
Browse files Browse the repository at this point in the history
…24806)

* fix added

* UI test added

* platform affected codes updated

* script file changes updated

* review changes updated

* Test case file changes updated

* tabbar automation id casing updated

* Automation ID changed for button

* unshipped file changes reverted

* changes updated

* platform condition updated

* UI test modified

* screen shot added

* access specifier changed
  • Loading branch information
devanathan-vaithiyanathan authored Sep 24, 2024
1 parent e0b3526 commit 6a6f383
Show file tree
Hide file tree
Showing 10 changed files with 120 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,30 @@ public override AView OnCreateView(LayoutInflater inflater, ViewGroup container,
return _rootView = root;
}

void OnShellContentPropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
if (e.PropertyName == ShellContent.TitleProperty.PropertyName && sender is ShellContent shellContent)
{
UpdateTabTitle(shellContent);
}
}

void UpdateTabTitle(ShellContent shellContent)
{
if (_tablayout == null || SectionController.GetItems().Count == 0)
return;

int index = SectionController.GetItems().IndexOf(shellContent);
if (index >= 0)
{
var tab = _tablayout.GetTabAt(index);
if (tab != null)
{
tab.SetText(new string(shellContent.Title));
}
}
}

void OnTabLayoutChange(object sender, AView.LayoutChangeEventArgs e)
{
if (_disposed)
Expand Down Expand Up @@ -327,13 +351,21 @@ void HookEvents()
SectionController.ItemsCollectionChanged += OnItemsCollectionChanged;
((IShellController)_shellContext.Shell).AddAppearanceObserver(this, ShellSection);
ShellSection.PropertyChanged += OnShellItemPropertyChanged;
foreach (var item in SectionController.GetItems())
{
item.PropertyChanged += OnShellContentPropertyChanged;
}
}

void UnhookEvents()
{
SectionController.ItemsCollectionChanged -= OnItemsCollectionChanged;
((IShellController)_shellContext?.Shell)?.RemoveAppearanceObserver(this);
ShellSection.PropertyChanged -= OnShellItemPropertyChanged;
foreach (var item in SectionController.GetItems())
{
item.PropertyChanged -= OnShellContentPropertyChanged;
}
}

protected virtual void OnPageSelected(int position)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Microsoft.Maui.Controls.Handlers
public partial class ShellContentHandler : ElementHandler<ShellContent, FrameworkElement>
{
public static PropertyMapper<ShellContent, ShellContentHandler> Mapper =
new PropertyMapper<ShellContent, ShellContentHandler>(ElementMapper);
new PropertyMapper<ShellContent, ShellContentHandler>(ElementMapper) { [nameof(ShellContent.Title)] = MapTitle };

public static CommandMapper<ShellContent, ShellContentHandler> CommandMapper =
new CommandMapper<ShellContent, ShellContentHandler>(ElementCommandMapper);
Expand All @@ -16,6 +16,14 @@ public ShellContentHandler() : base(Mapper, CommandMapper)
{
}

internal static void MapTitle(ShellContentHandler handler, ShellContent item)
{
var shellSection = item.Parent as ShellSection;
var shellItem = shellSection?.Parent as ShellItem;
var shellItemHandler = shellItem?.Handler as ShellItemHandler;
shellItemHandler?.UpdateTitle();
}

protected override FrameworkElement CreatePlatformElement()
{
return (VirtualView as IShellContentController).GetOrCreateContent().ToPlatform(MauiContext);
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
#nullable enable
#nullable enable
2 changes: 1 addition & 1 deletion src/Controls/src/Core/Shell/BaseShellItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public class BaseShellItem : NavigableElement, IPropertyPropagationController, I

/// <summary>Bindable property for <see cref="Title"/>.</summary>
public static readonly BindableProperty TitleProperty =
BindableProperty.Create(nameof(Title), typeof(string), typeof(BaseShellItem), null, BindingMode.OneTime, propertyChanged: OnTitlePropertyChanged);
BindableProperty.Create(nameof(Title), typeof(string), typeof(BaseShellItem), null, BindingMode.TwoWay, propertyChanged: OnTitlePropertyChanged);

/// <summary>Bindable property for <see cref="IsVisible"/>.</summary>
public static readonly BindableProperty IsVisibleProperty =
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
29 changes: 29 additions & 0 deletions src/Controls/tests/TestCases.HostApp/Issues/Issue7453.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?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.Issue7453"
FlyoutBehavior="Disabled"
Title="Issue7453">
<TabBar AutomationId="TabBar">
<Tab Title="Nested Tabs" AutomationId="tabbar">
<ShellContent x:Name="tab" Title="Home">
<ContentPage>
<StackLayout HorizontalOptions="Center" Spacing="30">
<HorizontalStackLayout>
<Label Text="Current Shell Title : " FontAttributes="Bold"/>
<Label Text="{Binding Source={x:Reference tab}, Path=Title}" AutomationId="LabelId" />
</HorizontalStackLayout>
<Button Text="Change Title" AutomationId="ChangeShellContentTitle" VerticalOptions="Center" HorizontalOptions="Center" WidthRequest="100" HeightRequest="40" Clicked="OnButtonClicked"/>
</StackLayout>
</ContentPage>
</ShellContent>

<ShellContent Title="Settings">
<ContentPage>
<Label Text="This is Settings page"/>
</ContentPage>
</ShellContent>
</Tab>
</TabBar>

</Shell>
16 changes: 16 additions & 0 deletions src/Controls/tests/TestCases.HostApp/Issues/Issue7453.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
namespace Maui.Controls.Sample.Issues;

[XamlCompilation(XamlCompilationOptions.Compile)]
[Issue(IssueTracker.Github, 7453, "ShellContent Title doesn't observe changes to bound properties", PlatformAffected.UWP | PlatformAffected.Android)]
public partial class Issue7453 : Shell
{
public Issue7453()
{
InitializeComponent();
}

private void OnButtonClicked(object sender, EventArgs e)
{
this.tab.Title = "Updated title";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#if !MACCATALYST
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Maui.Controls;
using NUnit.Framework;
using UITest.Appium;
using UITest.Core;

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

public override string Issue => "ShellContent Title doesn't observe changes to bound properties";

[Test]
[Category(UITestCategories.Shell)]
public void ChangeShellContentTitle()
{
App.WaitForElement("ChangeShellContentTitle");
App.Click("ChangeShellContentTitle");
VerifyScreenshot();
}
}
}
#endif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 6a6f383

Please sign in to comment.