From 784ab6ead385443f35f5017883ca71e3c5582afe Mon Sep 17 00:00:00 2001 From: Pedro Jesus Date: Mon, 9 Oct 2023 19:28:38 -0300 Subject: [PATCH 1/2] added shellExtensions class --- .../Library/Common/HostExtensions.cs | 2 + .../Library/Common/ShellExtensions.cs | 113 ++++++++++++++++++ 2 files changed, 115 insertions(+) create mode 100644 ShanedlerSamples/Library/Common/ShellExtensions.cs diff --git a/ShanedlerSamples/Library/Common/HostExtensions.cs b/ShanedlerSamples/Library/Common/HostExtensions.cs index a9dadf6..1916e3f 100644 --- a/ShanedlerSamples/Library/Common/HostExtensions.cs +++ b/ShanedlerSamples/Library/Common/HostExtensions.cs @@ -1,5 +1,6 @@ using Microsoft.Maui.Controls; using Microsoft.Maui.Handlers; +using ShanedlerSamples.Library.Common; using System; using System.Collections.Generic; using System.Linq; @@ -71,6 +72,7 @@ public static MauiAppBuilder ConfigureMauiWorkarounds(this MauiAppBuilder builde public static MauiAppBuilder ConfigureMauiWorkarounds(this MauiAppBuilder builder, bool addAllWorkaround) { + ShellToolbarExtensions.Init(); builder.ConfigureMauiHandlers(handlers => { #if ANDROID diff --git a/ShanedlerSamples/Library/Common/ShellExtensions.cs b/ShanedlerSamples/Library/Common/ShellExtensions.cs new file mode 100644 index 0000000..0b160cc --- /dev/null +++ b/ShanedlerSamples/Library/Common/ShellExtensions.cs @@ -0,0 +1,113 @@ +using Microsoft.Maui.Handlers; + +namespace ShanedlerSamples.Library.Common; +public static partial class ShellToolbarExtensions +{ + public static readonly BindableProperty ToolbarBackgroundColorProperty = + BindableProperty.Create("ToolbarBackgroundColor", typeof(Color), typeof(Shell), null, propertyChanged: OnBackgroundColorChanged); + + + + static bool TryGetToolbar(Shell shell, out Toolbar toolbar) + { + toolbar = null; + if (((IToolbarElement)shell).Toolbar is not Toolbar tb) + return false; + + toolbar = tb; + return true; + } + + static void OnBackgroundColorChanged(BindableObject bindable, object oldValue, object newValue) + { + var shell = Shell.Current; + + if (!shell.IsLoaded) + { + shell.Loaded += OnShellLoaded; + + void OnShellLoaded(object sender, EventArgs e) + { + var shell = (Shell)sender; + shell.Loaded -= OnShellLoaded; + + if (!TryGetToolbar(shell, out var toolbar)) + return; + + toolbar.BarBackground = (Color)newValue; + + shell.Navigated += OnShellNavigated; + } + + return; + } + + + if (!TryGetToolbar(shell, out var toolbar)) + return; + + toolbar.BarBackground = (Color)newValue; + } + + static void OnShellNavigated(object sender, ShellNavigatedEventArgs e) + { + var shell = (Shell)sender; + if (!TryGetToolbar(shell, out var toolbar)) + return; + + toolbar.Handler.UpdateValue("ToolbarBackgroundColor"); + } + + public static Color GetToolbarBackgroundColor(BindableObject element) + { + return (Color)element.GetValue(ToolbarBackgroundColorProperty); + } + + public static void SetToolbarBackgroundColor(BindableObject element, Color value) + { + element.SetValue(ToolbarBackgroundColorProperty, value); + } + + internal static void Init() + { + ToolbarHandler.Mapper.Add("ToolbarBackgroundColor", OnValueChanged); + // (h, v) => + //{ + //#if WINDOWS + // ((Toolbar)v).BarBackground = Colors.Fuchsia; + //#endif + //#if ANDROID + // var materialToolbar = h.PlatformView; + // materialToolbar.SetBackgroundColor(Colors.Fuchsia.ToPlatform()); + // materialToolbar.TextAlignment = Android.Views.TextAlignment.TextStart; + // var p = materialToolbar.Title; + // var z = materialToolbar.TitleFormatted; + //#endif + //}); + + ToolbarHandler.Mapper.AppendToMapping("Title", (h, v) => + { + OnValueChanged(h, v); + }); + } + + static async void OnValueChanged(IToolbarHandler handler, IToolbar toolbar) + { + var currentPage = Shell.Current.CurrentPage; + + if (currentPage is null) + { + return; + } + + var color = GetToolbarBackgroundColor(currentPage); + + if (color is null || toolbar is not Toolbar tb) + { + return; + } + + await Task.Delay(50); + tb.BarBackground = color; + } +} \ No newline at end of file From 322a325548194f10a5ab154dcd300acd3e99b12c Mon Sep 17 00:00:00 2001 From: Pedro Jesus Date: Mon, 9 Oct 2023 19:28:45 -0300 Subject: [PATCH 2/2] add it to the sample --- ShanedlerSamples/AppShell.xaml | 18 ++++------ ShanedlerSamples/KeyboardPage.xaml | 26 ++++++++------ ShanedlerSamples/MainPage.xaml | 58 +++++++++++++++++------------- ShanedlerSamples/MainPage.xaml.cs | 8 +++++ ShanedlerSamples/MauiProgram.cs | 19 ++++++++-- 5 files changed, 79 insertions(+), 50 deletions(-) diff --git a/ShanedlerSamples/AppShell.xaml b/ShanedlerSamples/AppShell.xaml index cac8e58..f202c13 100644 --- a/ShanedlerSamples/AppShell.xaml +++ b/ShanedlerSamples/AppShell.xaml @@ -3,25 +3,19 @@ x:Class="ShanedlerSamples.AppShell" xmlns="http://schemas.microsoft.com/dotnet/2021/maui" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" - xmlns:local="clr-namespace:ShanedlerSamples" xmlns:ios="clr-namespace:Maui.FixesAndWorkarounds.iOSSpecific" - ios:ShellAttachedProperties.PrefersLargeTitles="true" - xmlns:libary="clr-namespace:Maui.FixesAndWorkarounds"> + xmlns:libary="clr-namespace:Maui.FixesAndWorkarounds" + xmlns:local="clr-namespace:ShanedlerSamples" + ios:ShellAttachedProperties.PrefersLargeTitles="true"> - - + + - + diff --git a/ShanedlerSamples/KeyboardPage.xaml b/ShanedlerSamples/KeyboardPage.xaml index 7e89d6a..7260aa4 100644 --- a/ShanedlerSamples/KeyboardPage.xaml +++ b/ShanedlerSamples/KeyboardPage.xaml @@ -1,16 +1,22 @@  - - + + - + - - - + + +