1
votes

I have a problem when trying to load the Icon of a ToolbarItem in Xamarin: It doesn't show up. The toolbar certainly exists. I have handled the Click method and it works, but the Icon is not visible. the ".ico" file exists in all the "drawable" folders in the Android project. It's the same .ico file I use in another solution and there it works just fine as the icon of a ToolbarItem, but here it doesn't work. I have tried to load an embedded image, but it doesn't work either. If I set its Text property, it is shown properly. I have a HomePage which is a MasterDetailPage, and the detail of this page is RootPage. On RootPage I have the ToolbarItem. Here are my xaml codes and the code behind:

HomePage:

<?xml version="1.0" encoding="utf-8" ?>
<MasterDetailPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:views="clr-namespace:ITEC.Views;assembly=ITEC"
             x:Class="ITEC.Views.HomePage"
             IsPresented="True">

    <MasterDetailPage.Master>
        <ContentPage Title="Home" Padding="0,20,0,0">
            <StackLayout>
                <ListView x:Name="listView">
                    <ListView.ItemTemplate>
                        <DataTemplate>
                            <TextCell Text="{Binding Title}"/>
                        </DataTemplate>
                    </ListView.ItemTemplate>
                </ListView>
            </StackLayout>
        </ContentPage>
    </MasterDetailPage.Master>
    <MasterDetailPage.Detail>
        <views:RootPage/>
    </MasterDetailPage.Detail>
</MasterDetailPage>

Code behind:

using ITEC.Services;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace ITEC.Views
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class HomePage : MasterDetailPage
    {
        private OptionService _optionService;
        public HomePage()
        {
            InitializeComponent();
            Detail=new NavigationPage(new RootPage());
            _optionService = new OptionService();
            listView.ItemsSource = _optionService.GetOptions();
        }
    }
}

RootPage:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:embedded="clr-namespace:ITEC.MarkupExtensions"
             x:Class="ITEC.Views.RootPage">
    <ContentPage.ToolbarItems>
        <ToolbarItem Icon="settings.ico" Clicked="MenuItem_OnClicked"></ToolbarItem> 
    </ContentPage.ToolbarItems>
    <StackLayout>
        <Label>Hello world</Label>
    </StackLayout>
</ContentPage>

Code behind:

using System;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace ITEC.Views
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class RootPage : ContentPage
    {
        public RootPage()
        {
            InitializeComponent();
        }

        private void MenuItem_OnClicked(object sender, EventArgs e)
        {
            DisplayAlert("Hello", "OK", "No");
        }
    }
}
1
I made a basic demo, but didn't reproduced your problem. Could you please check the property of the .ico, see if it is added to your project as AndroidResource. If the problem persists, could you please share a basic demo that can reproduce this problem? - Elvis Xia - MSFT
@ElvisXia-MSFT it seems like this was the actual problem. Setting the build action to AndroidResource did the trick. When I copy-pasted the file from another project, I did not make sure if the file had the right build action. But isn't there any way to use an icon from embedded resource? It would be much cleaner that way, instead of moving one file to 6 different drawable folders - Stevie

1 Answers

0
votes

Try png file for your icon. i think it might be work.