Skip to content
This repository was archived by the owner on May 1, 2024. It is now read-only.

Fix UWP Toolbar icons #8147

Merged
merged 4 commits into from
Nov 4, 2019
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
@@ -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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
</PropertyGroup>
<ItemGroup>
<Compile Include="$(MSBuildThisFileDirectory)CollectionViewGroupTypeIssue.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue7505.cs" />
<Compile Include="$(MSBuildThisFileDirectory)CollectionViewItemsSourceTypes.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue1455.xaml.cs">
<DependentUpon>Issue1455.xaml</DependentUpon>
Expand Down
3 changes: 0 additions & 3 deletions Xamarin.Forms.Platform.UAP/ImageSourceIconElementConverter.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
using System;
using Windows.UI.Xaml.Controls;

namespace Xamarin.Forms.Platform.UWP
{
internal class ImageSourceIconElementConverter : Windows.UI.Xaml.Data.IValueConverter
{
public object Convert(object value, Type targetType, object parameter, string language)
{
// TODO: investigate whether we can use AsyncValue<T> instead of blocking

if (value is ImageSource source)
return source.ToWindowsIconElement();

Expand Down
17 changes: 16 additions & 1 deletion Xamarin.Forms.Platform.UAP/Platform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@
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
{
Expand Down Expand Up @@ -238,6 +240,7 @@ internal void UpdatePageSizes()
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()
{
Expand Down Expand Up @@ -434,7 +437,19 @@ internal async Task UpdateToolbarItems()

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);
Expand Down