--- /dev/null
+using System;
+using System.Collections.Generic;
+using System.Text;
+using Xamarin.Forms.Internals;
+using Xamarin.Forms.CustomAttributes;
+
+#if UITEST
+using Xamarin.UITest;
+using NUnit.Framework;
+using Xamarin.Forms.Core.UITests;
+#endif
+
+namespace Xamarin.Forms.Controls.Issues
+{
+ [Preserve(AllMembers = true)]
+ [Issue(IssueTracker.Github, 7181, "[Bug] Cannot update ToolbarItem text and icon", PlatformAffected.Android)]
+#if UITEST
+ [NUnit.Framework.Category(UITestCategories.Shell)]
+#endif
+ public class Issue7181 : TestShell
+ {
+ const string ToolbarBtn = "Toolbar button";
+ const string DefaultToolbarItemText = "Toolbar test";
+ const string AfterClickToolbarItemText = "Button Clicked";
+ const string SetToolbarIconBtn = "Set toolbar icon button";
+
+ int _clicks = 0;
+ ToolbarItem _toolbarItem;
+
+ protected override void Init()
+ {
+ var page = CreateContentPage("Test page");
+
+ _toolbarItem = new ToolbarItem(DefaultToolbarItemText, string.Empty, OnToolbarClicked)
+ {
+ AutomationId = ToolbarBtn
+ };
+
+ page.ToolbarItems.Add(_toolbarItem);
+ page.Content = new StackLayout()
+ {
+ Children =
+ {
+ new Label
+ {
+ Text = "You should be able to change toolbar text"
+ },
+ new Button
+ {
+ AutomationId = SetToolbarIconBtn,
+ Text = "Click to change toolbarIcon",
+ Command = new Command(()=> _toolbarItem.IconImageSource = "coffee.png" )
+ }
+ }
+ };
+ }
+
+ private void OnToolbarClicked() =>
+ _toolbarItem.Text = $"{AfterClickToolbarItemText} {_clicks++}";
+
+#if UITEST && __ANDROID__
+ [Test]
+ public void ShellToolbarItemTests()
+ {
+ var count = 0;
+ var toolbarButton = RunningApp.WaitForElement(ToolbarBtn);
+ Assert.AreEqual(toolbarButton[0].Text, DefaultToolbarItemText);
+
+ for (int i = 0; i < 5; i++)
+ {
+ RunningApp.Tap(ToolbarBtn);
+
+ toolbarButton = RunningApp.WaitForElement(ToolbarBtn);
+ Assert.AreEqual($"{AfterClickToolbarItemText} {count++}", toolbarButton[0].Text);
+ }
+
+ RunningApp.Tap(SetToolbarIconBtn);
+ }
+#endif
+ }
+}
{
if (_backButtonBehavior != null)
_backButtonBehavior.PropertyChanged -= OnBackButtonBehaviorChanged;
+
+ if(Page?.ToolbarItems?.Count > 0)
+ {
+ foreach (var item in Page.ToolbarItems)
+ item.PropertyChanged -= OnToolbarItemPropertyChanged;
+ }
+
((IShellController)_shellContext?.Shell)?.RemoveFlyoutBehaviorObserver(this);
UpdateTitleView(_shellContext.AndroidContext, _toolbar, null);
_searchView.SearchConfirmed -= OnSearchConfirmed;
_searchView.Dispose();
}
-
+
_drawerToggle?.Dispose();
}
{
var customIcon = await context.GetFormsDrawableAsync(image);
- if(customIcon != null)
+ if (customIcon != null)
icon = new FlyoutIconDrawerDrawable(context, tintColor, customIcon, text);
}
if (!String.IsNullOrWhiteSpace(text) && icon == null)
icon = new FlyoutIconDrawerDrawable(context, tintColor, icon, text);
- if(icon == null)
+ if (icon == null)
{
icon = new DrawerArrowDrawable(context.GetThemedContext());
icon.SetColorFilter(tintColor.ToAndroid(), PorterDuff.Mode.SrcAtop);
_drawerToggle.DrawerIndicatorEnabled = false;
toolbar.NavigationIcon = icon;
}
- else if(_flyoutBehavior == FlyoutBehavior.Flyout)
+ else if (_flyoutBehavior == FlyoutBehavior.Flyout)
{
_drawerToggle.DrawerIndicatorEnabled = isEnabled;
- if(isEnabled)
+ if (isEnabled)
{
_drawerToggle.DrawerArrowDrawable = icon;
toolbar.NavigationIcon = null;
{
toolbar.NavigationContentDescription = shell.FlyoutIcon.AutomationId;
}
- else if(toolbar.SetNavigationContentDescription(_shellContext.Shell.FlyoutIcon) == null)
+ else if (toolbar.SetNavigationContentDescription(_shellContext.Shell.FlyoutIcon) == null)
{
toolbar.SetNavigationContentDescription(R.String.Ok);
}
_titleViewContainer = null;
}
}
- else if(_titleViewContainer == null)
+ else if (_titleViewContainer == null)
{
_titleViewContainer = new ContainerView(context, titleView);
_titleViewContainer.MatchHeight = _titleViewContainer.MatchWidth = true;
foreach (var item in page.ToolbarItems)
{
+ item.PropertyChanged -= OnToolbarItemPropertyChanged;
+ item.PropertyChanged += OnToolbarItemPropertyChanged;
+
using (var title = new Java.Lang.String(item.Text))
{
var menuitem = menu.Add(title);
menuitem.SetShowAsAction(ShowAsAction.Always);
menuitem.SetOnMenuItemClickListener(new GenericMenuClickListener(((IMenuItemController)item).Activate));
-
- if(TintColor != Color.Default)
+
+ if (TintColor != Color.Default)
{
var view = toolbar.FindViewById(menuitem.ItemId);
- if (view is ATextView textView)
+ if (view is ATextView textView)
textView.SetTextColor(TintColor.ToAndroid());
}
menu.Dispose();
}
+ void OnToolbarItemPropertyChanged(object sender, PropertyChangedEventArgs e)
+ {
+ if (e.PropertyName == MenuItem.TextProperty.PropertyName)
+ UpdateToolbarItems();
+ if (e.PropertyName == MenuItem.IconImageSourceProperty.PropertyName)
+ UpdateToolbarItems();
+ }
+
void OnSearchViewAttachedToWindow(object sender, AView.ViewAttachedToWindowEventArgs e)
{
// We only need to do this tint hack when using collapsed search handlers