From 212ff1d4ca5742527472768c0843ecbca10d1788 Mon Sep 17 00:00:00 2001 From: Gerald Versluis Date: Mon, 4 Nov 2019 21:37:28 +0100 Subject: [PATCH] Fix UWP Toolbar icons (#8147) * Added demo and fix * Update Issue7505.cs * Update Platform.cs * Fixed AsyncValue --- .../Issue7505.cs | 47 ++++++++++++++++++++++ .../Xamarin.Forms.Controls.Issues.Shared.projitems | 1 + .../ImageSourceIconElementConverter.cs | 3 -- Xamarin.Forms.Platform.UAP/Platform.cs | 17 +++++++- 4 files changed, 64 insertions(+), 4 deletions(-) create mode 100644 Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Issue7505.cs diff --git a/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Issue7505.cs b/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Issue7505.cs new file mode 100644 index 0000000..1338f1c --- /dev/null +++ b/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Issue7505.cs @@ -0,0 +1,47 @@ +using Xamarin.Forms.CustomAttributes; +using Xamarin.Forms.Internals; + +namespace Xamarin.Forms.Controls.Issues +{ + [Preserve (AllMembers=true)] + [Issue (IssueTracker.Github, 7505, "Icons from Toolbaritem are not displayed on UWP if starts on second monitor", PlatformAffected.UWP)] + public class Issue7505 : MasterDetailPage + { + public Issue7505() + { + Master = new ContentPage { Title = "master" }; + Detail = CreateDetailPage("Don't look here, look at the toolbar!"); + } + + static Page CreateDetailPage(string text) + { + var page = new ContentPage { + Title = text, + Content = new StackLayout { + Children = { + new Label { + Text = "Pre-req: Have a multi-monitor setup with different resolutions. Then, start this app, drag it onto the secondary monitor and keep restarting it until you see the toolbar icons disappear. If they don't disappear, this works! Yes, it's a fun one.", + VerticalOptions = LayoutOptions.CenterAndExpand, + HorizontalOptions = LayoutOptions.CenterAndExpand, + } + } + } + }; + + var tbiBank = new ToolbarItem { Command = new Command (() => { }), IconImageSource = "bank.png" }; + var tbiCalc = new ToolbarItem { Command = new Command (() => { }), IconImageSource = "calculator.png" }; + var tbiXam = new ToolbarItem { Command = new Command (() => { }), IconImageSource = "xamarinlogo.png" }; + var tbiXamSecondary = new ToolbarItem { Command = new Command (() => { }), IconImageSource = "xamarinlogo.png", Order = ToolbarItemOrder.Secondary }; + var tbiCalcSecondary = new ToolbarItem { Command = new Command(() => { }), IconImageSource = "calculator.png", Order = ToolbarItemOrder.Secondary }; + + + page.ToolbarItems.Add (tbiBank); + page.ToolbarItems.Add (tbiCalc); + page.ToolbarItems.Add (tbiXam); + page.ToolbarItems.Add (tbiXamSecondary); + page.ToolbarItems.Add (tbiCalcSecondary); + + return new NavigationPage (page); + } + } +} diff --git a/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Xamarin.Forms.Controls.Issues.Shared.projitems b/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Xamarin.Forms.Controls.Issues.Shared.projitems index dd59c33..60ac858 100644 --- a/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Xamarin.Forms.Controls.Issues.Shared.projitems +++ b/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Xamarin.Forms.Controls.Issues.Shared.projitems @@ -10,6 +10,7 @@ + Issue1455.xaml diff --git a/Xamarin.Forms.Platform.UAP/ImageSourceIconElementConverter.cs b/Xamarin.Forms.Platform.UAP/ImageSourceIconElementConverter.cs index 1df6a44..fb984e3 100644 --- a/Xamarin.Forms.Platform.UAP/ImageSourceIconElementConverter.cs +++ b/Xamarin.Forms.Platform.UAP/ImageSourceIconElementConverter.cs @@ -1,5 +1,4 @@ using System; -using Windows.UI.Xaml.Controls; namespace Xamarin.Forms.Platform.UWP { @@ -7,8 +6,6 @@ namespace Xamarin.Forms.Platform.UWP { public object Convert(object value, Type targetType, object parameter, string language) { - // TODO: investigate whether we can use AsyncValue instead of blocking - if (value is ImageSource source) return source.ToWindowsIconElement(); diff --git a/Xamarin.Forms.Platform.UAP/Platform.cs b/Xamarin.Forms.Platform.UAP/Platform.cs index f5089ac..bfe38c5 100644 --- a/Xamarin.Forms.Platform.UAP/Platform.cs +++ b/Xamarin.Forms.Platform.UAP/Platform.cs @@ -11,8 +11,10 @@ using Windows.UI.ViewManagement; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Media; +using Windows.UI.Xaml.Media.Imaging; using Xamarin.Forms.Internals; using NativeAutomationProperties = Windows.UI.Xaml.Automation.AutomationProperties; +using WImage = Windows.UI.Xaml.Controls.Image; namespace Xamarin.Forms.Platform.UWP { @@ -238,6 +240,7 @@ namespace Xamarin.Forms.Platform.UWP Page _currentPage; readonly NavigationModel _navModel = new NavigationModel(); readonly ToolbarTracker _toolbarTracker = new ToolbarTracker(); + readonly ImageConverter _imageConverter = new ImageConverter(); readonly ImageSourceIconElementConverter _imageSourceIconElementConverter = new ImageSourceIconElementConverter(); Windows.UI.Xaml.Controls.ProgressBar GetBusyIndicator() { @@ -434,7 +437,19 @@ namespace Xamarin.Forms.Platform.UWP var button = new AppBarButton(); button.SetBinding(AppBarButton.LabelProperty, "Text"); - button.SetBinding(AppBarButton.IconProperty, "IconImageSource", _imageSourceIconElementConverter); + + if (commandBar.IsDynamicOverflowEnabled && item.Order == ToolbarItemOrder.Secondary) + { + button.SetBinding(AppBarButton.IconProperty, "IconImageSource", _imageSourceIconElementConverter); + } + else + { + var img = new WImage(); + img.SetBinding(WImage.SourceProperty, "Value"); + img.SetBinding(WImage.DataContextProperty, "IconImageSource", _imageConverter); + button.Content = img; + } + button.Command = new MenuItemCommand(item); button.DataContext = item; button.SetValue(NativeAutomationProperties.AutomationIdProperty, item.AutomationId); -- 2.7.4