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

Fix ShellContent Title doesn't observe changes to bound properties #24806

Merged
merged 15 commits into from
Sep 24, 2024
Merged
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 @@ -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.
Loading