2

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?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
ML13
  • 895
  • 8
  • 15
  • Try setting icon text without escaping `Text="\uf053"` – memsranga Nov 15 '18 at 23:29
  • @shanranm can you please elaborate more. – ML13 Nov 16 '18 at 04:34
  • Since you were able to get it working in Xaml, I'm assuming you've already written renderer for changing the toolbar item font (to font-awesome). Just set the `Text` property of `ToolbarItem` to the unicode value of the icon (in this case `\uf053`). – memsranga Nov 16 '18 at 07:22

2 Answers2

1

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.

R15
  • 13,982
  • 14
  • 97
  • 173
  • Thank you but how do you indicate that I want it to be an icon and NOT a text? – ML13 Nov 16 '18 at 13:44
  • We not need to know it. By the forms of string it is recognizing. Are you facing that conflict? – R15 Nov 16 '18 at 13:46
  • All I am getting is a marking for a icon (like a small square box with a x in the middle) but not the the actual font awesome icon I need – ML13 Nov 16 '18 at 14:19
  • I think you need to set `FontFamily`. For `Toolbar` you need to use custom renderer to do so, which would be more complicated. I'll suggest you here to use icons. for more information visit this https://stackoverflow.com/questions/52274361/xamarin-fontawesome-not-working-from-code-behind – R15 Nov 16 '18 at 14:40
  • If I do Text = ((char)0xf053).ToString(), at lest I get the square box mentioned above but if I do Icon = ((char)0xf270).ToString(), I get nothing – ML13 Nov 16 '18 at 15:14
  • @ML13 - I have updated my answer. Now I notice that it should have been `Icon` instead of `Text`. – R15 Nov 16 '18 at 16:11
0

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.

Markus Michel
  • 2,289
  • 10
  • 18