I want to use font awesome (http://fortawesome.github.io/Font-Awesome/design.html) in XAML.
I have been able to easily get it to work via direct XAML, by creating a fonts folder and adding the font there, then in XAML:
<TextBlock FontFamily="Fonts/#FontAwesome"></TextBlock>
Displays a martini glass icon.
However, when adding it programmatically it just shows and invalid symbol like so: []
, I tried the following:
XAML:
<TextBlock Name="textBlock"></TextBlock>
C#:
textBlock.FontFamily = new FontFamily("Fonts/#FontAwesome");
textBlock.Text = HttpUtility.HtmlDecode("");
and the following which returns the literal string:
textBlock.FontFamily = new FontFamily("Fonts/#FontAwesome");
textBlock.Text = "";
Any ideas?
How to: Use Special Characters in XAML Character Syntax Description < < Less than symbol. > > Greater than sign. & & Ampersand symbol. " " Double quote symbol. 1 more rows ...
Provides a lightweight control for displaying small amounts of text. For more info, design guidance, and code examples, see Text block. If you have the XAML Controls Gallery app installed, click here to open the app and see the TextBlock in action.
Text in WPF is always stored as Unicode. Adding extended characters (ones that are not on your keyboard) is easily done in XAML. Just learn this special Unicode syntax . For example, what if you need to add a few Greek characters to your text (π λ)?
Ampersand symbol. Double quote symbol. Single quote symbol. If you create a markup file using a text editor, such as Windows Notepad, you must save the file in the Unicode UTF-8 file format in order to preserve any encoded special characters. The following example shows how you can use special characters in text when creating markup. <!--
try the following :
textBlock.FontFamily= new FontFamily(new Uri("pack://application:,,,/"), @"/Fonts/#FontAwesome"); // you should well reference your font else you will get a square
textBlock.Text = "\uf000";// \u (unicode escape char) instead of &#x
and if you want to preview your textblock XAML use
t_out.Text = XamlWriter.Save(textBlock);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With