我想在我的 Xamarin 應用程式中制作一個帶有小圖示(來自 FontAwesome)和文本的按鈕。我知道我可以只制作一個按鈕,但問題是我需要兩種字體(文本的標準字體和圖示的 FontAwesome)。有沒有人碰巧知道我如何做到這一點,或者是否有另一種方法來實作我想要的?
謝謝!
uj5u.com熱心網友回復:
正如json提到的,我只是做了一個簡單的實作。
創建一個繼承自的新類Label,設定FormattedText為組合字串(標準和圖示),并在其上添加點擊手勢。
public class MyLabel : Label
{
public delegate void MyHandler(object sender, EventArgs e);
public event MyHandler myEvent;
public MyLabel(string _myIcon, string _myText)
{
//build the UI
FormattedString text = new FormattedString();
text.Spans.Add(new Span { Text = _myIcon ,FontFamily= "FontAwesome5Free-Regular" });
text.Spans.Add(new Span { Text = _myText, TextColor = Color.Red ,BackgroundColor = Color.Blue });
FormattedText = text;
//tap event
TapGestureRecognizer tap = new TapGestureRecognizer();
tap.Tapped = (sender,e) => {
myEvent(sender,e);
};
}
}
用法
MyLabel label = new MyLabel("", "test");
label.myEvent = (sener,e) =>
{
//do something when tapping
};
Content = label;
對于如何整合FontAwesome的Xamarin.Forms,請參閱
https://montemagno.com/xamarin-forms-custom-fonts-everywhere/。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/361139.html
上一篇:Xamarin.macOSNavigated事件不會為CustomWebView觸發
下一篇:XamariniOS:System.ExecutionEngineException。詳細資訊:在僅aot模式下運行時嘗試JIT編譯方法
