Error when try to customize font in xamarin.android

非 Y 不嫁゛ 提交于 2019-12-10 10:44:22

问题


I used xamarin.android for develop app, I tried to customize font with code:

 Typeface myfont = Typeface.CreateFromAsset (Context.Assets, "fonts/KhmerOS.ttf");

but when I built, it error as shown bellow:

Error CS0120: An object reference is required for the non-static field, method, or property 'Android.Content.Context.Assets.get' (CS0120)

please help me,

Best Regard Buntha


回答1:


This may be the late reply but might help someone with the same issue. Set BuilAction to Android Asset for font file. (Right click on fontfile->BuildAction select AndroidAsset)




回答2:


Try this:

Typeface tf = Typeface.CreateFromAsset(Application.Context.Assets, "fonts/KhmerOS.ttf");



回答3:


Try this :)

[assembly: ExportRenderer(typeof(Label), typeof(MyLabelRenderer))]
namespace App3.Droid.Debug
{
    class MyLabelRenderer : LabelRenderer
    {
        protected override void OnElementChanged(ElementChangedEventArgs<Label> e)
        {
        base.OnElementChanged(e);

        var label = (TextView)Control;
        Typeface font = Typeface.CreateFromAsset(Forms.Context.Assets, "your font name.otf/ttf");
        label.Typeface = font;
    }
}
}

Note: Put Fonts to Assets and set property "Build Action on AndroidAsset and Copy to Output Directory to Copy Always"




回答4:


Edit your code as

Typeface custom_font = Typeface.createFromAsset(getAssets(), "fonts/font name.ttf");
tx.setTypeface(custom_font);


来源:https://stackoverflow.com/questions/23307094/error-when-try-to-customize-font-in-xamarin-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!