1
我正在使用字體在我的移動應用程序中顯示圖標。問題是,當我將它直接寫入xaml文件時,字體圖標顯示正確,但在運行時將其設置爲不起作用。Xamarin表單:字體圖標在運行時不會呈現
我已經通過爲每個平臺創建自定義標籤控件和自定義渲染器來實現字體圖標。我在Android上面臨這個問題,還沒有在iOS上進行檢查。
自定義標籤:
using System;
using Xamarin.Forms;
namespace fonticon
{
public class IconLabel:Label
{
public IconLabel()
{
}
}
}
爲Android定製渲染:
using System;
using Android.Graphics;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;
// This informs the compiler that we're using this class to render an IconLabel on this platform
[assembly: ExportRenderer(typeof(fonticon.IconLabel), typeof(fonticon.Droid.IconLabelRenderer))]
namespace fonticon.Droid
{
public class IconLabelRenderer:LabelRenderer
{
// sets the font for the platform-specific ui component to be our custom font
protected override void OnElementChanged(ElementChangedEventArgs<Label> e)
{
base.OnElementChanged(e);
// Note we're using the filename here, NOT the font family name
var font = Typeface.CreateFromAsset(Forms.Context.ApplicationContext.Assets, "flatuiicons.ttf");
Control.Typeface = font;
}
}
}
下面的XAML工作:
<?xml version="1.0" encoding="utf-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:fonticon"
x:Class="fonticon.fonticonPage">
<StackLayout x:Name="mainContainer">
<local:IconLabel x:Name="lblTestIcon" Text="" VerticalOptions="Center" HorizontalOptions="Center" FontSize="40" />
</StackLayout>
</ContentPage>
下面的代碼無法正常工作:實施
lblTestIcon.Text = "";
來源是以下幾點:
您已覆蓋從文章第5步你貼?我已經錯過了這一步幾次,它只是導致它無法找到字體包.... 第5步。導入您的自定義字體到您的Android項目 右鍵單擊您的資產目錄在您的Android項目並選擇「添加文件」。導航到您在硬盤上選擇的字體文件並將其添加到項目中。添加文件後,右鍵單擊它並選中「Build Action」設置爲「AndroidAsset」。 –
這個問題是通過使用文章中提到的以下代碼修復的,但不知何故,我錯過了它。 Text = System.Net.WebUtility.HtmlDecode(「&#xe012;」); – dsakpal