ToolbarItem in the code behind

ML13FBML13FB Member ✭✭
edited November 2018 in General

I am using Xamarin using font-awesome 4.7 version, I am trying to add a ToolbarItem in the code behind

var toolBarItem = new ToolbarItem
{
Icon ="",
}

In xaml file I can do something this:

<assets:Icon Text="&#xf053;" TextColor="#5DD046" FontSize="24" HorizontalOptions="Start" VerticalTextAlignment="Center" Margin="25,0,0,0"/>

But I need it to work in the code behind, can someone advise me how to go about it?

Answers

  • JohnHardmanJohnHardman GBUniversity mod

    @ML13FB said:
    But I need it to work in the code behind, can someone advise me how to go about it?

    This is effectively what I currently do (although with some added workarounds for bugs that have hopefully been fixed by now):

            ToolbarItem toolbarItem = new ToolbarItem
            {
                ClassId = classId,
                Order = order,
                Priority = priority,
                Icon = icon, // I pass a FileImageSource here
                Text = text, // this is a string to be displayed
                AutomationId = automationId,
                CommandParameter = commandParameter,
                Command = command,
            };
    

    That should give you an idea of the properties to set if using ToolbarItem.

    (currently, as I am in the process of replacing the XF toolbar implementation by one of my own)

  • ML13FBML13FB Member ✭✭

    @JohnHardman Thank you, but I need it to be a pure icon with no text at all. My problem is that doesn't actually display.

  • JohnHardmanJohnHardman GBUniversity mod

    @ML13FB said:
    @JohnHardman Thank you, but I need it to be a pure icon with no text at all. My problem is that doesn't actually display.

    Make sure you set the Icon property with a FileImageSource. If you're not sure whether the FileImageSource is picking up an image correctly, try displaying an Image using it.

    If you're sure the FileImageSource is ok, the next question is no which platforms do you not see the icon, and are you populating a Primary or Secondary toolbar?

  • ML13FBML13FB Member ✭✭

    @JohnHardman said:
    Make sure you set the Icon property with a FileImageSource. If you're not sure whether the FileImageSource is picking up an image correctly, try displaying an Image using it.

    Can you please elaboarte how to set the Icon with FileImageSource. I need it for both both Primary and Secondary.

  • JohnHardmanJohnHardman GBUniversity mod

    @ML13FB said:
    Can you please elaboarte how to set the Icon with FileImageSource

    The Charles Petzold book, available free online at https://docs.microsoft.com/en-us/xamarin/xamarin-forms/creating-mobile-apps-xamarin-forms/ , does pretty good coverage of FileImageSource

Sign In or Register to comment.