0

I have a Xamarin Forms app. I would like to get a Toolbaritem icon to display colors and detail.

Just for an example, I am using https://commons.wikimedia.org/wiki/File:Cambodia_road_sign_PW03_W2_07.png as a test.

Here is how it displays on Android using <ToolbarItem IconImageSource="Cambodia_road_sign_PW03_W2_07.png" Order="Primary" Priority="0" />:

enter image description here

Note the lack of color and details. On UWP on the other hand it displays colors and detail like this:

enter image description here

I don't think the resolution is wrong in some way for Android because the same file, used as a FlyoutItem icon, displays correctly:

enter image description here

These images are from the Windows Android emulator but a real Android device gives the same results.

FWIW, I do not have an iOS implementation at this time, so cannot say what happens on iOS.

Any ideas?

Charles
  • 479
  • 1
  • 3
  • 13

1 Answers1

0

I download the icon with 16px to check. The code below works for me.

  <ContentPage.ToolbarItems>
    <ToolbarItem IconImageSource="Cambodia_road_sign__16px.png">         
    </ToolbarItem>
</ContentPage.ToolbarItems>

Use the code below to show the bar.

 MainPage = new NavigationPage(new ShellPage());

enter image description here

Update:

For Shell, you could use the Shell.TitleView. Set it inside all your Flyout ContentPage.

Shell:

<?xml version="1.0" encoding="utf-8" ?>
<Shell xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
   xmlns:views="clr-namespace:App2_XF"
   
         x:Class="App2_XF.ShellPage">


<FlyoutItem FlyoutDisplayOptions="AsMultipleItems"    >
    <ShellContent Title="Item1" ContentTemplate="{DataTemplate views:Page1}"></ShellContent>
    <ShellContent Title="Item2" ContentTemplate="{DataTemplate views:Page2}"></ShellContent>
</FlyoutItem>
</Shell>

Page1: Add TitleView for both Page1, Page2

   <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         xmlns:xct="http://xamarin.com/schemas/2020/toolkit"
         xmlns:android="http://schemas.android.com/apk/res/android"
         x:Class="App2_XF.Page1">
 <Shell.TitleView>
    <StackLayout>
        <Image HorizontalOptions="End" VerticalOptions="End" HeightRequest="40" WidthRequest="40" Source="Cambodia_road_sign__16px.png"></Image>
    </StackLayout>
</Shell.TitleView>
<ContentPage.Content>
   ......
</ContentPage.Content>
</ContentPage>

enter image description here

Wendy Zang - MSFT
  • 10,509
  • 1
  • 7
  • 17
  • thank you. What exactly do you mean by "download the icon with 16px"? Also, I don't explicitly do MainPage = new etc. I use the AppShell flyout menu that came from a Xamarin template (so I may have to play with that aspect a little). – Charles Jun 16 '22 at 12:11
  • I just tested with a 16 x16 icon I happenned to have and I get the same results. The icon has color and detail but what appears in the Toolbar is a simple, monochrome silhouette. – Charles Jun 16 '22 at 13:01
  • The way the page is loaded is the key! When I load my test page using an explicit new NavigationPage() the icon renders correctly. But my app is built using the Flyout menu template. How do I get the Flyout menu to load the page in such a way that the icon displays correctly? – Charles Jun 16 '22 at 13:15
  • Check my update. – Wendy Zang - MSFT Jun 17 '22 at 01:53
  • Well the good news is yes, that solves the Android display problem. The bad news is that now the icon does not display at all on UWP. Any ideas (other than an elaborate On Platform dance)? The other bad news is that now the ContentPage Title goes away. Yes, I guess I can get that back with a Label in the TitleView, but this is getting to be quite the device-specific kluge. Also, I lose the Clicked method that ToolbarIterms have, but I can work around and/or live with that. – Charles Jun 17 '22 at 16:51
  • https://github.com/xamarin/Xamarin.Forms/issues/13875 – Charles Jun 17 '22 at 19:13
  • Well, working all day on this I think I have it. I tried to post my code but it says "Edit queue is full" (whatever that means). Anyway, I have a Shell.TitleView with the Image and a Label (to replace the Page Title, which is gone on Android). They are in a Grid so I can have them side by side. Then in ToolbarItems I have the icon (for UWP, where TitleView does not work) conditioned with an OnPlatform. I have an open question on SO about OnPlatform. – Charles Jun 17 '22 at 22:19