我想在XAML中使用font awesome(
http://fortawesome.github.io/Font-Awesome/design.html).
我已经能够通过直接XAML,通过创建一个字体文件夹并在那里添加字体,然后在XAML中轻松地使它工作:
<TextBlock FontFamily="Fonts/#FontAwesome"></TextBlock>
显示马提尼玻璃图标.
但是,当以编程方式添加它时,它只显示和无效的符号,如:[],我尝试了以下内容:
XAML:
<TextBlock Name="textBlock"></TextBlock>
C#:
textBlock.FontFamily = new FontFamily("Fonts/#FontAwesome"); textBlock.Text = HttpUtility.HtmlDecode("");
textBlock.FontFamily = new FontFamily("Fonts/#FontAwesome"); textBlock.Text = "";
有任何想法吗?
解决方法
尝试以下方法:
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
如果你想预览文本块XAML使用
t_out.Text = XamlWriter.Save(textBlock);