Xamarin addind ToolbarItem in dynamically in C#

2k views Asked by At

I am new with Xamarin.

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?

2

There are 2 answers

6
Arvind Chourasiya On

From code behind you can do like this

Icon = "\uf053"

Or

Icon = ((char)0xf053).ToString();

Your original values is "&#xf053;", so for all values the same rule applies to change.

0
Markus Michel On

You have created a toolbar item but in order to appear, it has to be added to the page. So in your page code you should call

this.ToolbarItems.Add(toolBarItem);

after creating the item.